-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLeftRotation.java
41 lines (34 loc) · 1.18 KB
/
LeftRotation.java
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
package arrays;
import java.util.Scanner;
public class LeftRotation {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int length = scanner.nextInt();
int rotations = scanner.nextInt() % length;
int[] array = getArray(length);
array = rotateArray(array, rotations);
print(array);
}
private static int[] getArray(int length) {
int[] array = new int[length];
for (int index = 0 ; index < array.length ; index++) {
array[index] = scanner.nextInt();
}
return array;
}
private static void print(int[] array) {
for (int number : array) {
System.out.print(number + " ");
}
}
private static int[] rotateArray(int[] array, int rotations) {
if (rotations == 0) {
return array;
}
int[] result = new int[array.length];
if (array.length - rotations >= 0)
System.arraycopy(array, rotations, result, 0, array.length - rotations);
if (rotations >= 0) System.arraycopy(array, 0, result, -rotations + array.length, rotations);
return result;
}
}