Skip to content

Commit 08fa28c

Browse files
author
Antesh Sharma
committed
Array of int rotation to left and right by d elements
1 parent 8abee66 commit 08fa28c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/main/java/com/antesh/problems/ArrayRotation.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ public static void rotateRight(int[] arr, int d) {
2020
int n = arr.length;
2121
int j = 0;
2222
while (j < d) {
23-
int temp = arr[n - 1];
24-
for (int i = 0; i < n; i++) {
25-
int current = arr[i];
26-
arr[i] = temp;
27-
temp = current;
28-
29-
}
23+
rotateByOnePosition(arr, n);
3024
j++;
3125
}
3226
}
3327

28+
private static void rotateByOnePosition(int[] arr, int n) {
29+
int temp = arr[n - 1];
30+
for (int i = 0; i < n; i++) {
31+
int current = arr[i];
32+
arr[i] = temp;
33+
temp = current;
34+
}
35+
}
36+
3437
public static void rotateLeft(int[] arr, int d) {
3538
if (arr == null || arr.length == 0) {
3639
return;

0 commit comments

Comments
 (0)