what is the concept behind taking gcd(input,no_of_rotations)
Juggling Algorithm Doubt
yeah i referred it
void leftRotate( int arr[], int d, int n)
{
/* To handle if d >= n */
d = d % n;
int g_c_d = gcd(d, n);
for ( int i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
int temp = arr[i];
int j = i;
while (1) {
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break ;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
i was not getting the line if(k==i) break;
@dare_devil_007 you dont want to go into an infinite loop! there is no condition present in the while loop, its while(1) so a condition is added to break the loop when k == i (this is the last element to be swapped). You should dry run this algorithm for more clarification, it will really help
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.