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?
- A. 8 cycles of 1
- B. 1 cycle of 12
- C. 3 cycles of 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Interpolation search (pos = lo + (key − a[lo]) · (hi − lo) / (a[hi] − a[lo]), integer division) looks for 32 in [1, 2, 4, 8, 16, 32, 64…
- What is the sum of min(subarray) over all contiguous subarrays of [2, 5, 1, 4, 3]? (A monotonic stack counts, for each element, how many…
- What is the length of the longest substring of ')()())(()())' that is a valid (balanced) parentheses string?
- The stock span of day i is the number of consecutive days ending at day i whose price is ≤ the price on day i. For prices [31, 27, 14, 21…
- What is the minimum number of swaps (of any two elements) needed to sort the permutation [4, 3, 1, 2, 6, 5] into ascending order?
- A stack-based decoder expands k[s] into s repeated k times (nesting allowed). What does '2[x3[y]z]q' decode to?