The following code is supposed to rotate the array A by B positions.

So, for example,

A : [1 2 3 4 5 6]

B : 1

The output :

[2 3 4 5 6 1]

However, there is a small bug in the problem. Fix the bug and submit the problem.

Skip to content
# Tag: InterviewStreet

## Array Rotation: bug detection

## Color Sort Problem

### Solution

## GCD algorithm in Java

## HackerRank: Find Single Integer out of an Array

## Problem Statement

## Solution

## HackerRank: Sherlock and The Beast

## Problem Statement

## Solution:

## 2D Array: Hour Glass problem solution

## Problem Statement:

## Solution:

## HackerRank: Chocolate in Box

## Problem Statement:

## HackerRank: Sparse Arrays

## Problem Statement

## Solution

The following code is supposed to rotate the array A by B positions.

So, for example,

A : [1 2 3 4 5 6]

B : 1

The output :

[2 3 4 5 6 1]

However, there is a small bug in the problem. Fix the bug and submit the problem.

Given an array with n objects colored red, white or blue,

sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note: Using library sort function is not allowed.

Example :

Input : [0 1 2 0 1 2]

Modify array so that it becomes : [0 0 1 1 2 2]

Given 2 non negative integers m and n, find gcd(m, n)

GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n.

Both m and n fit in a 32 bit signed integer.

Example

m : 6

n : 9

GCD(m, n) : 3

NOTE : DO NOT USE LIBRARY FUNCTIONS

Given an array of integers, every element appears twice except for one. Find that single one.

*Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?*

**Example :**

```
Input : [1 2 2 3 1]
Output : 3
```

Logic:

The basic logic that A XOR A = 0 means that means all the doubles will be XOR’ed out to 0 and the remaining number will be the result of the XOR.

Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again plotting something diabolical. Sherlock’s companion, Dr. Watson, suggests Moriarty may be responsible for MI6’s recent issues with their supercomputer, *The Beast*.

Shortly after resolving to investigate, Sherlock receives a note from Moriarty boasting about infecting *The Beast*with a virus; however, he also gives him a clue—a number, NN. Sherlock determines the key to removing the virus is to find the largest *Decent Number* having NN digits.

A *Decent Number* has the following properties:

- Its digits can only be
*3*‘s and/or*5*‘s. - The number of
*3*‘s it contains is divisible by*5*. - The number of
*5*‘s it contains is divisible by*3*. - If there are more than one such number, we pick the largest one.

Moriarty’s virus shows a clock counting down to *The Beast*‘s destruction, and time is running out fast. Your task is to help Sherlock find the key before *The Beast* is destroyed!

**Constraints**

1≤T≤201≤T≤20

1≤N≤1000001≤N≤100000

**Input Format**

The first line is an integer, TT, denoting the number of test cases.

The TT subsequent lines each contain an integer, NN, detailing the number of digits in the number.

**Output Format**

Print the largest Decent Number having NN digits; if no such number exists, tell Sherlock by printing **-1**.

**Sample Input**

```
4
1
3
5
11
```

**Sample Output**

```
-1
555
33333
55555533333
```

**Explanation**

For N=1, there is no decent number having 1 digit (so we print −1−1).

For N=3, 555 is the only possible number. The number 5 appears three times in this number, so our count of 5‘s is evenly divisible by 3 (*Decent Number Property 3*).

For N=5, 33333 is the only possible number. The number 3 appears five times in this number, so our count of 3‘s is evenly divisible by 5 (*Decent Number Property 2*).

For N=11, 55555533333 and all permutations of these digits are valid numbers; among them, the given number is the largest one.

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
```

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 *i ^{th}* container has

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* ≤ 10^{6}

1 ≤ *A[i]* ≤ 10^{9}

**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
```

Output:

321143

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+2^{nd} line contains Q, the number of queries.

The following Q lines each contain a query string.

**Constraints**

1≤N≤1000

1≤Q≤1000

1≤length of any string≤20

**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.