Juggling-Algorithm Description and Code added
parent
77f7934109
commit
a89bbd56ce
|
@ -1,3 +1,17 @@
|
||||||
|
/* In this method, divide the array into M sets, where M = GCD (n, k), and then rotate the elements in each set.
|
||||||
|
From the number of elements ( n ) of the array and number of rotations ( k ) to be made to the array, the GCD(n, k) number of blocks are made.
|
||||||
|
Then in each block, shifting will take place to the corresponding elements in the block.
|
||||||
|
After all the elements in all the blocks are shifted, the array will be rotated for the given number of times.
|
||||||
|
Example: If we want to rotate the array Arr : {10, 20, 30, 40, 50, 60} by 2 positions :
|
||||||
|
M = GCD(60, 20) = 20
|
||||||
|
Initial Array : 10 20 30 40 50 60
|
||||||
|
First Set Moves : 50 20 10 40 30 60
|
||||||
|
Second Set Moves : 50 60 10 20 30 40
|
||||||
|
Time complexity : O(N)
|
||||||
|
Auxiliary Space : O(1)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
int gcd(int a, int b) {
|
int gcd(int a, int b) {
|
||||||
|
|
Loading…
Reference in New Issue