forked from itsjohnty/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateArrayJava
34 lines (34 loc) · 832 Bytes
/
RotateArrayJava
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
26
27
28
29
30
31
32
33
34
class Solve {
public static void rotate(int[] nums, int k) {
k %= nums.length;
int n = nums.length;
int i = 0 , j = n-1;
while(i<j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;j--;
}
i = 0; j = k-1;
while(i<j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;j--;
}
i = k; j = nums.length-1;
while(i<j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;j--;
}
}
public static void main (String[] args) {
int[] arr = {1,2,3,4,5,6,7};
rotate (arr,3);
for(int i = 0; i<arr.length;i++ ){
System.out.print(arr[i]);
}
}
}