Does the Juggling algorithm support right rotation?

Viewed 319

I know that juggling works for left rotation. Does right rotation fit for juggling algorithm ? If so, then what has to be modified for it to work for right rotation?

Left rotation using juggling algorithm

Reference taken from 3rd method on the below link

/*Function to left rotate arr[] of siz n by d*/ 

void leftRotate(int arr[], int d, int n) { int i, j, k, temp; for (i = 0; i < gcd(d, n); i++) { /* move i-th values of blocks */ temp = arr[i]; j = i; while(1) { k = j + d; if (k >= n) k = k - n; if (k == i) break; arr[j] = arr[k]; j = k; } arr[j] = temp; } }

Pls help me whether it supports or not

1 Answers

A good approach to convert algorithms like this is to check if there any symmetry (or other homomorphisms) between what you have working and what you want.

Here, you can observe that flipping the array direction, running the algorithm, and flipping back would work. So, you can simply replace any read/write to arr[x] by a matching read/write to arr[(n-1) - x]

Related