Juggling algorithm
Array rotation
Data structures
Algorithm efficiency
Computer science

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.

Practice algorithms

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

  1. Compute the GCD: Calculate the GCD of `n` and `d`. This determines the number of cycles you'll need to perform the rotation.
  2. Cycle Replacements: For each element in a cycle, move the current item to its new position `d` steps away.
  3. 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`.

  1. Calculate GCD: GCD(9, 3) = 3. This means the array will be divided into 3 cycles.
  2. Cycle 1 (starting from index 0):
    1 -> 4 -> 7 -> 1
    2 -> 5 -> 8 -> 2
    3 -> 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, d=dmodnd = d \mod n, simplifying the rotation direction and length.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.