Data Structures & AlgorithmsHardTCS Digital · Infosys pattern

The juggling algorithm rotates an array of n = 12 elements left by k = 8 positions by following cycles of the form i → i + k (mod n). How many cycles does it process, and how many elements are in each?

  1. A. 8 cycles of 1
  2. B. 1 cycle of 12
  3. C. 3 cycles of 4
  4. D. 4 cycles of 3

Correct answer: D. 4 cycles of 3

The number of cycles is gcd(n, k) = gcd(12, 8) = 4, each of length n / gcd = 3: e.g. 0 → 8 → 4 → 0. '3 cycles of 4' swaps the two numbers; a single cycle happens only when gcd(n, k) = 1.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic