-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjuggling algorithm.cpp
More file actions
54 lines (46 loc) · 946 Bytes
/
juggling algorithm.cpp
File metadata and controls
54 lines (46 loc) · 946 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
48
49
50
51
52
53
54
/*
code by harsha_76
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef std::vector<int> vi;
#define all(x) x.begin(), x.end()
#define rep(i, start, end) for(int i=start;i<end;i++)
void juggle(vector<int>& a, int n, int d)
{
d=d%n;
int g=__gcd(n, d);
for(int i=0;i<g;i++)
{
int temp=a[i];
int k=i+d;
int j=i;
while(k!=i)
{
a[j]=a[k];
j=k;
k=(k+d)%n;
}
a[j]=temp;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; printf("n = "); scanf("%d",&n);
int d; printf("d = "); scanf("%d",&d);
vi a(n);
printf("elements : \n");
for(int i=0;i<n;i++) cin>>a[i];
juggle(a, n, d);
cout<<"rotated array\n";
for(int e:a)
{
cout<<e<<' ';
}
cout<<'\n';
return 0;
}