-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRotateArray.java
More file actions
25 lines (21 loc) · 803 Bytes
/
RotateArray.java
File metadata and controls
25 lines (21 loc) · 803 Bytes
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
import java.util.Arrays;
public class RotateArray {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
rotate(array, 3);
System.out.println(Arrays.toString(array));
}
public static void rotate(int[] array, int rotations) {
rotations = rotations % array.length;
int[] result = new int[array.length];
for (int index = 0 ; index < array.length - rotations ; index++) {
result[index + rotations] = array[index];
}
for (int index = array.length - rotations ; index < array.length ; index++) {
result[index + rotations - array.length] = array[index];
}
for (int index = 0 ; index < array.length ; index++) {
array[index] = result[index];
}
}
}