-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayRotation.java
More file actions
79 lines (60 loc) · 1.71 KB
/
Copy pathArrayRotation.java
File metadata and controls
79 lines (60 loc) · 1.71 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package LinkedInQuestions.Arrays;
/**
* Created by rmukherj on 8/12/16.
*/
public class ArrayRotation {
public void rotateLeft(){
int[] arr = {1,2,3,4,5,6,7,8};
int orderToRotate = 2;
for(int i=0;i<orderToRotate;i++){
for(int j=arr.length-1;j>0;j--){
int temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
}
for(int j=0;j<arr.length;j++){
System.out.println(arr[j]);
}
}
private void optimizedRotateLeft(){
int[] arr = {1,2,3,4,5,6,7,8};
int order =2;
int offset = arr.length - order%arr.length;
if(offset>0){
int[] copy = arr.clone();
for(int i=0;i<arr.length;++i){
int j= (i+offset)%arr.length;
arr[i] = copy[j];
System.out.print(arr[i]);
}
}
}
//rotate array using reverse strategy
public void rotate(int[] nums, int k){
k= k % nums.length;
reverse(nums,0,nums.length-1);
reverse(nums,0,k-1);
reverse(nums,k,nums.length-1);
}
private void reverse(int[] nums, int start, int end){
while(start<end){
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
public static void main(String[] args) {
ArrayRotation arr = new ArrayRotation();
//arr.rotateLeft();
//arr.optimizedRotateLeft();
//arr.optimizedRotateLeft();
int[] a = {1,2,3,4,5,6,7,8};
arr.rotate(a,2);
for(int j=0;j<a.length;j++){
System.out.print(a[j]);
}
}
}