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

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/feed-rss2.php on line 8
Post – Rajesh http://tech.learnerandtutor.com A Developer Sat, 09 Jul 2022 14:19:29 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Arrays: Left Rotation http://tech.learnerandtutor.com/arrays-left-rotation/ http://tech.learnerandtutor.com/arrays-left-rotation/#respond Fri, 05 Feb 2021 01:00:00 +0000 https://learnerandtutor.com/?p=138 Continue reading ]]>

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]);
        }
    }

}
]]>
http://tech.learnerandtutor.com/arrays-left-rotation/feed/ 0
2D Array – DS – HourGlass http://tech.learnerandtutor.com/2d-array-ds-hourglass/ http://tech.learnerandtutor.com/2d-array-ds-hourglass/#respond Thu, 04 Feb 2021 01:00:00 +0000 https://learnerandtutor.com/?p=131

Related posts:

  1. Jumping on the Clouds
  2. Sales by Match
  3. Counting Valleys
  4. Repeated String
  5. Arrays: Left Rotation
]]>
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);
    }
}

]]>
http://tech.learnerandtutor.com/2d-array-ds-hourglass/feed/ 0
Repeated String http://tech.learnerandtutor.com/repeated-string/ http://tech.learnerandtutor.com/repeated-string/#respond Wed, 03 Feb 2021 01:00:00 +0000 https://learnerandtutor.com/?p=125

Related posts:

  1. Jumping on the Clouds
  2. Sales by Match
  3. Counting Valleys
  4. 2D Array – DS – HourGlass
  5. Arrays: Left Rotation
]]>
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);
    }
}
]]>
http://tech.learnerandtutor.com/repeated-string/feed/ 0
Jumping on the Clouds http://tech.learnerandtutor.com/jumping-on-the-clouds/ http://tech.learnerandtutor.com/jumping-on-the-clouds/#respond Tue, 02 Feb 2021 01:00:00 +0000 https://learnerandtutor.com/?p=111

Related posts:

  1. Sales by Match
  2. Counting Valleys
  3. Repeated String
  4. 2D Array – DS – HourGlass
  5. Arrays: Left Rotation
]]>
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);
    }
}
]]>
http://tech.learnerandtutor.com/jumping-on-the-clouds/feed/ 0
Counting Valleys http://tech.learnerandtutor.com/counting-valleys/ http://tech.learnerandtutor.com/counting-valleys/#respond Mon, 01 Feb 2021 01:00:00 +0000 https://learnerandtutor.com/?p=107

Related posts:

  1. Jumping on the Clouds
  2. Sales by Match
  3. Repeated String
  4. 2D Array – DS – HourGlass
  5. Arrays: Left Rotation
]]>
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);
    }
}

]]>
http://tech.learnerandtutor.com/counting-valleys/feed/ 0
Sales by Match http://tech.learnerandtutor.com/sales-by-match/ http://tech.learnerandtutor.com/sales-by-match/#respond Tue, 12 Jan 2021 10:30:49 +0000 https://learnerandtutor.com/?p=96 Continue reading ]]> 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.

]]>
http://tech.learnerandtutor.com/sales-by-match/feed/ 0
A journey to learn http://tech.learnerandtutor.com/a-journey/ http://tech.learnerandtutor.com/a-journey/#respond Mon, 04 Jan 2021 16:58:36 +0000 https://learnerandtutor.com/?p=1 Continue reading ]]> Few months back I came to know about the book ‘Cracking the Coding Interview’ by ‘Gayle Laakmann McDowell‘. The book is mainly about the interview process of top companies and 189 programming questions to crack those interviews. The reviews about this book were good as well. So I thought of reading this book. So I ordered through amazon.

I received the book few weeks later and kept it in the book-shelf. I didn’t read it. On this 2021 new year suddenly I got a spark. Why don’t we read this book now and write about those learning as posts in a blog?

The outcome is this blog. The idea is simple. I will read this book and explore all the coding challenges from this book. Whatever I learn, will post it here. So that interested programmers either they are preparing for interviews or learning on their own may benefit.

Thanks.

]]>
http://tech.learnerandtutor.com/a-journey/feed/ 0