Rotating an array using Juggling algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Rotating an array is a common problem in computer science, involving the repositioning of elements in a list. One of the most interesting and efficient methods to solve this is the Juggling algorithm. This technique is particularly useful for its elegant use of cyclic replacements to achieve rotation without requiring additional memory for a secondary data structure, making it space-efficient.
Understanding the Problem
Given an array `arr` of size `n` and a number `d`, rotate the array to the left by `d` positions. For example, if `arr = [1, 2, 3, 4, 5, 6, 7]` and `d = 2`, the resulting array after rotation would be `[3, 4, 5, 6, 7, 1, 2]`.
The Juggling Algorithm
The Juggling algorithm employs a set of cyclic rotations based on the greatest common divisor (GCD) of `n` (the size of the array) and `d` (the number of positions to rotate).
Steps Involved
- Compute the GCD: Calculate the GCD of `n` and `d`. This determines the number of cycles you'll need to perform the rotation.
- Cycle Replacements: For each element in a cycle, move the current item to its new position `d` steps away.
- Continue Cycles: Repeat these cyclic movements for all sets determined by the GCD.
Technical Explanation
The operation's efficiency stems from the cyclical permutation pattern of the GCD, reducing the problem to a series of manageable rotations over fixed cycles. The primary advantage of this technique is that each element is moved a minimal number of times, ideally once, resulting in an `O(n)` time complexity and `O(1)` space complexity since the operation is performed in-place.
Example
Consider an array `arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]` with `n = 9` and `d = 3`.
- Calculate GCD: GCD(9, 3) = 3. This means the array will be divided into 3 cycles.
- Cycle 1 (starting from index 0):1 -> 4 -> 7 -> 12 -> 5 -> 8 -> 23 -> 6 -> 9 -> 3
- Right Rotation: The Juggling algorithm can also be adapted for right rotations by effectively rotating the array to the left by `n-d` positions.
- Edge Cases: Handle scenarios where `d` is 0 or `d` equals `n`, as these imply no rotation.
- Modulo Operation: If `d > n`, reduce `d` using modulo operation, , simplifying the rotation direction and length.
Related reading
- Rotating right an array of int in c?
- Roulette wheel selection algorithm
- Round robin - dynamic weights
- Round Robin Tournament algorithm in C
- Rough set Quick reduct/ feature selection in Python
- Rounding a list of values to the nearest value from another list in python
- Round to the nearest power of two
- Run ML algorithm inside map function in Spark

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.