Skip to content

Commit f028092

Browse files
solves rotate array
1 parent 385989a commit f028092

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: src/RotateArray.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Arrays;
2+
3+
public class RotateArray {
4+
public static void main(String[] args) {
5+
int[] array = {1, 2, 3, 4, 5};
6+
rotate(array, 3);
7+
System.out.println(Arrays.toString(array));
8+
}
9+
10+
public static void rotate(int[] array, int rotations) {
11+
rotations = rotations % array.length;
12+
int[] result = new int[array.length];
13+
for (int index = 0 ; index < array.length - rotations ; index++) {
14+
result[index + rotations] = array[index];
15+
}
16+
17+
for (int index = array.length - rotations ; index < array.length ; index++) {
18+
result[index + rotations - array.length] = array[index];
19+
}
20+
21+
for (int index = 0 ; index < array.length ; index++) {
22+
array[index] = result[index];
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)