2D Array: Hour Glass problem solution

Problem Statement:

You are given a 2D array with dimensions 6*6. An hourglass in an array is defined as a portion shaped like this:

a b c
  d
e f g

For example, if we create an hourglass using the number 1 within an array full of zeros, it may look like this:

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

Actually, there are many hourglasses in the array above. The three topmost hourglasses are the following:

1 1 1     1 1 0     1 0 0
  1         0         0
1 1 1     1 1 0     1 0 0

The sum of an hourglass is the sum of all the numbers within it. The sum for the hourglasses above are 7, 4, and 2, respectively.

In this problem, you have to print the largest sum among all the hourglasses in the array.

Note: If you have already solved the problem “Java 2D array” in the data structures chapter of the Java domain, you may skip this challenge.

Input Format

There will be exactly 6 lines of input, each containing 6 integers separated by spaces. Each integer will be between -9 and 9, inclusively.

Output Format

Print the answer to this problem on a single line.

Sample Input

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

Sample Output

19

Explanation

The hourglass possessing the largest sum is:

2 4 4
  2
1 2 4

Solution:

HackerRank: Chocolate in Box

Problem Statement:

Dexter and Debra are playing a game. They have N containers each having one or more chocolates. Containers are numbered from 1 to N, where ith container has A[i] number of chocolates.

The game goes like this. First player will choose a container and take one or more chocolates from it. Then, second player will choose a non-empty container and take one or more chocolates from it. And then they alternate turns. This process will continue, until one of the players is not able to take any chocolates (because no chocolates are left). One who is not able to take any chocolates loses the game. Note that player can choose only non-empty container.

The game between Dexter and Debra has just started, and Dexter has got the first Chance. He wants to know the number of ways to make a first move such that under optimal play, the first player always wins.

Input Format
The first line contains an integer N, i.e., number of containers.
The second line contains N integers, i.e., number of chocolates in each of the containers separated by a single space.

Output Format
Print the number of ways to make the first move such that under optimal play, the first player always wins. If the first player cannot win under optimal play, print 0.

Constraints
1 ≤ N ≤ 106
1 ≤ A[i] ≤ 109

Sample Input

2
2 3

Sample Output

1

Explanation

Only 1 set of moves helps player 1 win.

Player:      1      2      1      2      1
Chocolates: 2 3 -> 2 2 -> 1 2 -> 1 1 -> 0 1

Sample Data

Output:

321143

Theory Data for the solution and algorithm

HackerRank: Sparse Arrays

Problem Statement

There are N strings. Each string’s length is no more than 2020 characters. There are also Q queries. For each query, you are given a string, and you need to find out how many times this string occurred previously.

Input Format

The first line contains N, the number of strings.
The next N lines each contain a string.
The N+2nd line contains Q, the number of queries.
The following Q lines each contain a query string.

Constraints

1N1000
1Q1000
1lengtof anstring20

Sample Input

4
aba
baba
aba
xzxb
3
aba
xzxb
ab

Sample Output

2
1
0

Explanation

Here, “aba” occurs twice, in the first and third string. The string “xzxb” occurs once in the fourth string, and “ab” does not occur at all.

Solution

Bubble Sort Algorithm

Below is the code to do Bubble Sorting in Java for integer values

public class BubbleSort {
  
    // logic to sort the elements
    public static void srtbubble(int array[]) {
        int n = array.length;
        int k;
        for (int m = n; m >= 0; m--) {
            for (int i = 0; i < n - 1; i++) { k = i + 1; if (array[i] > array[k]) {
                    swap(i, k, array);
                }
            }
            print(array);
        }
    }
  
    private static void swap(int i, int j, int[] array) {
  
        int temp;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
  
    private static void print(int[] input) {
          
        for (int i = 0; i < input.length; i++) {
            System.out.print(input[i] + ", ");
        }
        System.out.println("\n");
    }
  
    public static void main(String[] args) {
        int[] input = { 14, 21, 19, 62, 233, 142, 134, 10, 41 };
        srtbubble(input);
    }
}


Output:

14, 19, 21, 62, 142, 134, 10, 41, 233, 

14, 19, 21, 62, 134, 10, 41, 142, 233, 

14, 19, 21, 62, 10, 41, 134, 142, 233, 

14, 19, 21, 10, 41, 62, 134, 142, 233, 

14, 19, 10, 21, 41, 62, 134, 142, 233, 

14, 10, 19, 21, 41, 62, 134, 142, 233, 

10, 14, 19, 21, 41, 62, 134, 142, 233, 

10, 14, 19, 21, 41, 62, 134, 142, 233, 

10, 14, 19, 21, 41, 62, 134, 142, 233, 

10, 14, 19, 21, 41, 62, 134, 142, 233,

Finding loop in a singly linked-list

You can detect it by simply running two pointers through the list.
Start the first pointer p1 on the first node and the second pointer p2 on the second node.

Advance the first pointer by one every time through the loop, advance the second pointer by two. If there is a loop, they will eventually point to the same node. If there’s no loop, you’ll eventually hit the end with the advance-by-two pointer.

Consider the following loop:

head -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
                  ^                        |
                  |                        |
                  +------------------------+

Starting A at 1 and B at 2, they take on the following values:

p1   p2
=    =
1    2
2    4
3    6
4    8
5    4
6    6

Because they’re equal, and P2 should always be beyond p1 in a non-looping list (because it’s advancing by two as opposed to the advance-by-one behavior of p1), it means you’ve discovered a loop.

The pseudo-code will go something like this:

  • Start with hn(Head Node )of the linked list
  • If hn==null; return false; //empty list
  • if hn.next!=null
    • p1=hn; p2=hn;
    • While p2!=null loop
      • p1=p1.next//Advancing p1 by 1
      • if p2.next!=null
        • p2=p2.next.next//Advancing p2 by 2
        • else return false
      • if p1==p2
        • return true// Loop was found
    • return false// till now no loop was found

Once you know a node within the loop, there’s an O(n) guaranteed method to find the start of the loop.

Let’s return to the original position after you’ve found an element somewhere in the loop but you’re not sure where the start of the loop is.

                                 p1,p2 (this is where p1 and p2
                                 |               first met).
                                 v
head -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
                  ^                        |
                  |                        |
                  +------------------------+

This is the process to follow:

  • First, advance p2 and set the loopsize to 1.
  • Second, while p1 and p2 are not equal, continue to advance p2, increasing the loopsize each time. That gives the size of the loop, six in this case.
    If the loopsize ends up as 1, you know that you must already be at the start of the loop, so simply return A as the start, and skip the rest of the steps below.
  • Third, simply set both p1 and p2 to the first element then advance p2 exactly loopsize times (to the 7 in this case). This gives two pointers that are different by the size of the loop.
  • Lastly, while p1 and p2 are not equal, you advance them together. Since they remain exactly loopsize elements apart from each other at all times, p1 will enter the loop at exactly the same time as p2 returns to the start of the loop. You can see that with the following walk through:
    • loopsize is evaluated as 6
    • set both p1 and p2 to 1
    • advance p2 by loopsize elements to 7
    • 1 and 7 aren’t equal so advance both
    • 2 and 8 aren’t equal so advance both
    • 3 and 3 are equal so that is your loop start

Now, since each those operations are O(n) and performed sequentially, the whole thing is O(n).

Source

Algorithm Name: Floyd–Warshall algorithm