-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotationOfArray.java
More file actions
43 lines (36 loc) · 814 Bytes
/
RotationOfArray.java
File metadata and controls
43 lines (36 loc) · 814 Bytes
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
public class Solution {
public static void rotate(int[] arr, int d) {
int small[] = new int[d];
for(int i =0;i<d;i++)
{
small[i] = arr[i];
}// 2 6
int i = 0,j=arr.length-1;
while(i<j)
{
int temp = arr[j];
arr[j]=arr[i];
arr[i]=temp;
i++;
j--;
}// 8 9 5 1 3 6 2
int a = 0,b = arr.length-d-1;
while(a<b)
{
int temp = arr[b];
arr[b]=arr[a];
arr[a]=temp;
a++;
b--;
}
int y = arr.length-d,z=arr.length-1;
while(y<z)
{
int temp = arr[z];
arr[z]=arr[y];
arr[y]=temp;
y++;
z--;
}
}
}