-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathJ_33_rorateArrayBY_K_position.java
More file actions
40 lines (32 loc) · 1.04 KB
/
J_33_rorateArrayBY_K_position.java
File metadata and controls
40 lines (32 loc) · 1.04 KB
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
35
36
37
38
39
40
public class J_33_rorateArrayBY_K_position {
public static void reverseArr(int arr[], int low, int high){
int mid = (low+high)/2;
int temp;
for( int i = low,x=0; i <= mid; i++,x++){
temp = arr[i];
arr[i] = arr[high-x];
arr[high-x] = temp;
}
// 1 2 3 4 5 6 7 8 9
// 1 2 3 9 5 6 7 8 4
// 1 2 3 9 8 6 7 5 4
// 1 2 3 9 8 7 6 5 4
}
public static void leftRotate(int arr[], int k){
reverseArr(arr, k, arr.length-1);
reverseArr(arr, 0, k-1);
reverseArr(arr, 0, arr.length-1);
}
public static void showArr(int arr[]){
System.out.println();
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}System.out.println();
}
public static void main(String[] args) {
// 0 1 2 3 4 5 6 7 8 9
int arr[] = {1,2,3,4,5,6,7,8,9,10};
leftRotate(arr, 4);
showArr(arr);
}
}