-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathRotation_of_Arrays.cpp
More file actions
47 lines (37 loc) · 901 Bytes
/
Rotation_of_Arrays.cpp
File metadata and controls
47 lines (37 loc) · 901 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
44
45
46
47
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
//Function to calculate the GCD of n and k.
int gcd(int a,int b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
int main()
{
int n,k,d,j,temp;
//Input the values of n and k.
/*here n is the size of array and k is the number
of positions to be rotated*/
cin>>n>>k;
int a[n];
for(int m=0;m<n;m++){
cin>>a[m];
}
for(int i=0;i<gcd(n,k);i++){ // Invoking the GCD function.
j=i;
temp=a[i];
while(1){
d=(j+k)%n;
if(d==i)
break;
a[j]=a[d];
j=d;
}
a[j]=temp;
}
for(int m=0;m<n;m++){
cout<<a[m]<<" ";
}
}