Skip to content

Commit 765f949

Browse files
committed
Fix for resolving Time Complexity in Rotating Array
1 parent d371bda commit 765f949

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎src/Arrays/RotatingArray.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ public static void main(String[] args) {
77

88
int [] array = {1,2,3,4,5};
99

10-
rotateArray(array,2);
10+
rotateArray(array,97);
1111
}
1212

1313
static void rotateArray(int [] array, int rotation){
1414

1515
int lengthOfArray = array.length;
1616

17+
if(rotation % lengthOfArray == 0){
18+
rotation = lengthOfArray;
19+
}else{
20+
rotation = rotation%lengthOfArray;
21+
}
22+
23+
System.out.println(rotation);
24+
1725
for (int j = 1; j <= rotation; j++) {
1826

1927
int temp = array[lengthOfArray -1];
@@ -22,6 +30,7 @@ static void rotateArray(int [] array, int rotation){
2230
array[i+1] = array[i];
2331
}
2432
array[0] = temp;
33+
// System.out.println(Arrays.toString(array));
2534
}
2635

2736
System.out.println(Arrays.toString(array));

0 commit comments

Comments
 (0)