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: Algorithm

## 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:

## HackerRank: DynamicArray

## Problem Statement

## Solution:

## Bubble Sort Algorithm

## Combinations of a String

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.

There are **N** sequences. All of them are initially empty, and you are given a variable **lastans = 0**. You are given **Q** queries of two different types:

- “
**1 x y**” – Insert**y**at the end of the**((x XOR lastans) mod N)**^{th}sequence. - “
**2 x y**” – Print the value of the**(y mod size)**^{th}element of the**((x XOR lastans) mod N)**^{th}sequence. Here, $size$ denotes the size of the related sequence. Then, assign this integer to**lastans**.

**Note**: You may assume that, for the second type of query, the related sequence will not be an empty sequence. Sequences and the elements of each sequence are indexed by zero-based numbering.

You can get more information about XOR from Wikipedia. It is defined as **^** in most of the modern programming languages.

**Input Format**

The first line consists of $N$, number of sequences, and $Q$, number of queries, separated by a space. The following $Q$ lines contains one of the query types described above.

**Constraints**

**1 < N,Q < 10^5**

**0 < x < 10^9**

**0 < y < 10^9**

**Output Format**

For each query of type two, print the answer on a new line.

**Sample Input**

```
2 5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1
```

**Sample Output**

```
7
3
```

**Explanation**

The first sequence is **5, 3** and the second sequence is **7**.

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,

**Problem:
**Write an algorithm to print all possible combinations of characters in a string.

**Solution:
**Since we need to generate combinations, we can start with a single character and then continue to add a character to combinations we have seen so far.

Let’s use “XYZ” as an example.

public void buildTree(String input, StringBuffer output, int k) { for (int i = k; i < input.length(); i++) { output.append(input.charAt(i)); buildTree(input, output, i + 1); output.deleteCharAt(output.length() - 1); } } public static void main(String[] args){ buildTree("XYZ", new StringBuffer(), 0); }

Dry Run just to give an idea:

X–>Y–>Z

–>Z–>YY–>X–>Z

–>Z–>XZ–>Y–>X

–>X–>Y