-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy patharrayRotate.java
More file actions
36 lines (32 loc) · 874 Bytes
/
arrayRotate.java
File metadata and controls
36 lines (32 loc) · 874 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
class arrayRotate {
public static int gcd(int a, int b) {
if(b==0)
return a;
else
return gcd(b, a%b);
}
public static void main(int arr[], int k) {
System.out.println("Original array : ");
for(int ele:arr)
System.out.print(ele+" ");
int l = arr.length, temp, m;
int gcd = gcd(l,k);
for(int i = 0;i < gcd;i++) {
temp = arr[i];
int gap=i;
while(true) {
m = gap+k;
if(m >= l)
m -= l;
if(m==i)
break;
arr[gap] = arr[m];
gap = m;
}
arr[gap]=temp;
}
System.out.println("\nRoatated array : ");
for (int i=0;i<l;i++)
System.out.print(arr[i]+" ");
}
}