diff --git a/algorithms/CPlusPlus/Arrays/Juggling-Algorithm.cpp b/algorithms/CPlusPlus/Arrays/Juggling-Algorithm.cpp index d979a12d..9a76f56c 100644 --- a/algorithms/CPlusPlus/Arrays/Juggling-Algorithm.cpp +++ b/algorithms/CPlusPlus/Arrays/Juggling-Algorithm.cpp @@ -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 using namespace std; int gcd(int a, int b) {