Skip to content

Commit eea7371

Browse files
committed
Solution to rotate an Array N number of Times
1 parent 472be3f commit eea7371

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎src/Arrays/RotatingArray.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Arrays;
2+
3+
import java.util.Arrays;
4+
5+
public class RotatingArray {
6+
public static void main(String[] args) {
7+
8+
int [] array = {1,2,3,4,5};
9+
10+
rotateArray(array,2);
11+
}
12+
13+
static void rotateArray(int [] array, int rotation){
14+
15+
int lengthOfArray = array.length;
16+
17+
for (int j = 1; j <= rotation; j++) {
18+
19+
int temp = array[lengthOfArray -1];
20+
21+
for (int i = lengthOfArray -2; i >= 0; i--) {
22+
array[i+1] = array[i];
23+
}
24+
array[0] = temp;
25+
}
26+
27+
System.out.println(Arrays.toString(array));
28+
}
29+
}

0 commit comments

Comments
 (0)