Right Rotation
# Topic
Iterating over arrays
# Problem
Right Rotation
A right rotation is an operation that shifts each element of an array to the right. For example, if an array is {1,2,3,4,5} and we right rotate it by 1, the new array will be {5,1,2,3,4}. If we rotate it by 2, the new array will be {4,5,1,2,3}. It goes like this: {1,2,3,4,5} -> {5,1,2,3,4} -> {4,5,1,2,3}.
Write a program that performs a right rotation on an array by a given number.
Note that If your solution gets the code quality warning "System.arraycopy is more efficient", please simply ignore it in respect of this code challenge.
Input format: The first line is an array of numbers. The second line is the number of rotations.
Output format: Resulting array
Sample Input 1:
1 2 3 4 5
1
2
Sample Output 1:
5 1 2 3 4
Sample Input 2:
1 2 3 4 5
2
2
Sample Output 2:
4 5 1 2 3
Sample Input 3:
1 2 3 4 5
8
2
Sample Output 3:
3 4 5 1 2
Sample Input 4:
11 21 1 41 51 78 90
4
2
Sample Output 4:
41 51 78 90 11 21 1
# Hint & Explain
You can create array of Strings in this way:
String[] entrance = scanner.nextLine().split(" ");
Spaces will be to divide each element of array.
# Solution
# My solution
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// put your code here
Scanner scanner = new Scanner(System.in);
String[] array = scanner.nextLine().split(" ");
int times = scanner.nextInt();
String before;
String after = array[0];
for (int j = 1; j <= times; j++) {
array[0] = array[array.length - 1];
for (int i = 0; i < array.length; i++) {
if (i + 1 < array.length) {
before = after;
after = array[i + 1];
array[i + 1] = before;
}
}
}
for (String item :
array) {
System.out.print(item + " ");
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Other solution 1
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] array = scanner.nextLine().split(" ");
int arrayShift = scanner.nextInt() % array.length;
String[] newArray = new String[array.length];
for (int i = 0; i < newArray.length; i++) {
int index = (arrayShift + i) % array.length;
newArray[index] = array[i];
}
for (String element : newArray) {
System.out.print(element + " ");
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Other solution 2
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
int step = scanner.nextInt();
String[] row = s.split(" ");
int len = row.length;
int remember = step % len;
String temp;
for (int j = 0; j < remember; j++) {
temp = row[len - 1];
System.arraycopy(row, 0, row, 1, len - 2 + 1);
row[0] = temp;
}
String str = String.join(" ", row);
System.out.println(str);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Other solution 3
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
String[] array = scanner.nextLine().split(" ");
int value = scanner.nextInt();
for (String string : array) {
int num = Integer.parseInt(string);
list.add(num);
}
Collections.rotate(list, value);
for (Integer num : list) {
System.out.print(num + " ");
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24