Deprecated: Creation of dynamic property WPtouchProFour::$settings_object is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php on line 82

Deprecated: Automatic conversion of false to array is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/admin-load.php on line 70

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property YARPP::$is_custom_template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php on line 56

Deprecated: Creation of dynamic property YARPP::$db_options is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php on line 69
Rajesh | A Developer

Arrays: Left Rotation

Problem

A left rotation operation on an array shifts each of the array’s elements 1 unit to the left. For example, if 2 left rotations are performed on an array [1,2,3,4,5], then the array would become [3,4,5,1,2]. Note that the lowest index item move to the highest index in a rotation. This is called circular array.

Given an array a of n integers and a number, d perform d left rotations on the array. Return the updated array to be printer as a single line of space-separated integers.

Function Description

Complete the function rotLeft in the editor below.

rotLeft has the following parameter(s):

  • int a[n]: the array to rotate
  • int d: the number of rotations

Returns

  • int a'[n]: the rotated array

Input Format

  • The first line contains two space-separated integers n and d, the size of a and the number of left rotations.
  • The second line contains n space-separated integers, each an a[i]

Constraints

  • 1 <= n <= 105
  • 1 <= d <= n
  • 1 <= a[i] <=106

Solution

package org.span;

import java.io.IOException;

public class ArrayLeftRotation {

    static int[] rotLeft(int[] a, int d) {
        for (int i = 0; i < d; i++) {
            int t = a[0];
            System.arraycopy(a, 1, a, 0, a.length - 1);
            a[a.length - 1] = t;
        }
        return a;
    }

    public static void main(String[] args) throws IOException {
        int d = 4;
        int[] a = {1, 2, 3, 4, 5};
        int[] result = rotLeft(a, d);
        print(result);
    }


    public static void print(int[] a) {
        for (int i = 0; i < a.length; i++) {
            if (i > 0)
                System.out.print(" " + a[i]);
            else
                System.out.print(a[i]);
        }
    }

}
Posted in Post | Tagged | Leave a comment

2D Array – DS – HourGlass

Problem

Solution

package org.span;

import java.io.IOException;
import java.util.Scanner;

public class HourGlass {

    static int hourglassSum(int[][] arr) {
        int max = -99999, cur;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                cur = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
                if (cur > max) {
                    max = cur;
                }
            }
        }
        return max;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        /*int[][] arr = {
                {1, 1, 1, 0, 0, 0},
                {0, 1, 0, 0, 0, 0},
                {1, 1, 1, 0, 0, 0},
                {0, 0, 2, 4, 4, 0},
                {0, 0, 0, 2, 0, 0},
                {0, 0, 1, 2, 4, 0}
        };*/
        int[][] arr = {
                {0, -4, -6, 0, -7, -6},
                {-1, -2, -6, -8, -3, -1},
                {-8, -4, -2, -8, -8, -6},
                {-3, -1, -2, -5, -7, -4},
                {-3, -5, -3, -6, -6, -6},
                {-3, -6, 0, -8, -6, -7},
        };
        int result = hourglassSum(arr);
        System.out.println(result);
    }
}

Posted in Post | Tagged | Leave a comment

Repeated String

Problem

Solution

import java.io.IOException;

public class RepeatedString {
    static long repeatedString(String s, long n) {
        long noOfOccurence = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'a')
                noOfOccurence++;
        }
        long repeat = n / s.length();
        noOfOccurence *= repeat;
        for (int i = 0; i < n % s.length(); i++) {
            if (s.charAt(i) == 'a')
                noOfOccurence++;
        }
        return noOfOccurence;
    }

    public static void main(String[] args) throws IOException {
        String s = "aba";
        long n = 10;
        long result = repeatedString(s, n);
        System.out.println(result);
    }
}
Posted in Post | Tagged | Leave a comment

Jumping on the Clouds

Problem

Solution

import java.io.IOException;

public class JumpingOnClouds {
    static int jumpingOnClouds(int[] c) {
        int jumps = 0;
        for (int i = 0; i < c.length; ) {
            if (i + 2 < c.length && c[i + 2] == 0)
                i += 2;
            else
                i++;
            jumps++;
        }
        return jumps - 1;
    }

    public static void main(String[] args) throws IOException {
        int[] c = {0, 0, 1, 0, 0, 1, 0};
        int result = jumpingOnClouds(c);
        System.out.println(result);
    }
}
Posted in Post | Tagged | Leave a comment

Counting Valleys

Problem

Solution

import java.io.IOException;

class Result {
    public static int countingValleys(int steps, String path) {
        int valleyCount = 0, prePos, pos = 0;
        for (int i = 0; i < steps; i++) {
            prePos = pos;
            if (path.charAt(i) == 'D')
                pos--;
            else
                pos++;
            if (pos == 0 & prePos < 0)
                valleyCount++;
        }
        return valleyCount;
    }
}

public class CountingValleys {
    public static void main(String[] args) throws IOException {
        int steps = 8;
        String path = "UDDDUDUU";
        int result = Result.countingValleys(steps, path);
        System.out.println(result);
    }
}

Posted in Post | Tagged | Leave a comment

Sales by Match

Problem

Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale.
Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

For example, there are n=7 socks with colors ar = [1,2,1,2,1,3,2] .
There is one pair of color 1 and one of color 2.
There are three odd socks left, one of each color. The number of pairs is 2.

Function Description
Complete the sockMerchant function in the editor below.
It must return an integer representing the number of matching pairs of socks that are available.

sockMerchant has the following parameter(s):
n: the number of socks in the pile
ar: the colors of each sock

Input Format
The first line contains an integer n, the number of socks represented in ar.
The second line contains n space-separated integers describing the colors ar[i] of the socks in the pile.

Constraints
1 <= n <= 100
1 <= ar[i] <= 100 where 0 <= i < n

Output Format
Return the total number of matching pairs of socks that Alex can sell.

Sample Input
9
10 20 20 10 10 30 50 10 20

Sample Output
3

Alex can match three pairs of socks

Solution

import java.util.HashMap;
import java.util.Scanner;

public class PairOfSocks {
    static int sockMerchant(int n, int[] ar) {
        HashMap<Integer, Integer> colorsMap = new HashMap<>();
        for (int color : ar) {
            colorsMap.put(color, (colorsMap.getOrDefault(color, 0)) + 1);
        }
        final int[] totalPair = {0};
        colorsMap.forEach((key, value) -> totalPair[0] += value / 2);
        return totalPair[0];
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        int[] ar = new int[n];
        String[] arItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int arItem = Integer.parseInt(arItems[i]);
            ar[i] = arItem;
        }
        int result = sockMerchant(n, ar);
        System.out.println(result);
        scanner.close();
    }
}

Explanation

We need to find no of pairs for each number. We can add the quantity of each number into a map. Then we can divide the values by 2 and sum up to get the final answer.

Posted in Post | Tagged | Leave a comment