-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJuggling_algo.cpp
More file actions
49 lines (47 loc) · 1.04 KB
/
Juggling_algo.cpp
File metadata and controls
49 lines (47 loc) · 1.04 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
//This algorithm is used to solve the rotate array by k postion problem
//Problem:Suppose we are given an array of n integers and we have to rotate it by k positions to the left with space complexity of O(1) i.e. within the same array (in-place).
//Input:-a[]=1,2,3,4,5,6,7,8,9 ,n=9; k=3;
//Output:-4,5,6,7,8,9,1,2,3
//CODE:
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
void rotateArray(int a[],int n ,int k)
{
int d=-1,i,temp,j;
for(int i=0;i<gcd(n,k);i++) //number of sets depend on gcd of(n,k)
{
j=i;
temp=a[i];
while(1)
{
d=(j+k)%n; //finds the position to which the value is shifted to left
if(d==i) //if d has reachd the position of i which means set is completed
break;
a[j]=a[d];
j=d;
}
a[j]=temp;
}
}
void displayArray(int a[],int n)
{
int i;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<"\n";
}
int main()
{
int a[]={1,2,3,4,5,6,7,8,9};
int n=sizeof(a)/sizeof(a[0]);
rotateArray(a,n,2);
displayArray(a,n);
return 0;
}