An array A consists of n integers in locations A[0], A[1] ....A[n-1]. It is…
2024
An array A consists of n integers in locations A[0], A[1] ....A[n-1]. It is required to shift the elements of the array cyclically to the left by k places, where 1 <= k <= (n-1). An incomplete algorithm for doing this in linear time, without using another array is given below. Complete the algorithm by filling in the blanks. Assume all the variables are suitably declared.
min = n; i = 0;
while (___________) {
temp = A[i]; j = i;
while (________) {
A[j] = ________
j= (j + k) mod n ;
If ( j< min ) then
min = j;
}
A[(n + i - k) mod n] = _________
i = __________
- A.
i > min; j!= (n+i)mod n; A[j + k]; temp; i + 1 ;
- B.
i < min; j!= (n+i)mod n; A[j + k]; temp; i + 1;
- C.
i > min; j!= (n+i+k)mod n; A[(j + k)]; temp; i + 1;
- D.
i < min; j!= (n+i-k)mod n; A[(j + k)mod n]; temp; i + 1;
Show answer & explanation
Correct answer: D
Concept: This is the in-place "cycle-following" (juggling) algorithm for a cyclic left rotation by k, done in O(n) time with no auxiliary array. Under the map j -> (j+k) mod n, the n index positions split into gcd(n,k) disjoint cycles. Each cycle can be resolved by walking it once starting from any unvisited index: save the value at the start in temp, keep overwriting A[j] with the value k slots ahead until the walk returns to the start, then drop temp into the last freed slot. A running pointer (min) is used to know when every index has been swept into some cycle, so the outer loop can keep launching a fresh cycle from the next unvisited start.
Application to this fragment:
Outer loop condition: i starts at 0 and min starts at n (out of range), and the outer loop must keep starting new cycles until every index has been visited, i.e. until i catches up with min. That test is i < min, which fixes the first blank and rules out any option built on i > min (that condition is false from the very first check, so the loop would never run at all).
Inner loop condition: inside a cycle we walk j -> (j+k) mod n starting at j = i. The index right before the walk wraps back to i is the predecessor of i under this map, namely (n + i - k) mod n. So the inner loop must keep going while j != (n + i - k) mod n. This rules out the option whose inner test is built on (n + i) mod n: that expression simplifies to i, and since j is set equal to i on the previous line, the test is false immediately, so that inner loop body would never execute even once.
Assignment inside the loop: each element must be moved back k slots with the index wrapped at the array boundary, i.e. A[j] = A[(j + k) mod n]. Dropping the mod n (writing A[j + k] or A[(j + k)] without the wrap) lets the index run past n-1 once j + k reaches or exceeds n, which is out of bounds rather than a rotation.
After the inner loop ends, the one value that was saved in temp before the cycle got overwritten must be written into the last freed slot, which is exactly A[(n + i - k) mod n] = temp.
Finally i = i + 1 advances the outer loop, and here is precisely why that is safe. Inside a cycle starting at index i, the code compares j against min only AFTER advancing it to (j+k) mod n - the cycle's own starting index i is never itself compared. For the very first cycle (i = 0), the values that DO get compared are every other member of index 0's residue class modulo g = gcd(n,k), namely g, 2g, ..., n-g; the smallest of these is g, so min becomes exactly g by the time that cycle finishes. Every later cycle (starting at i = 1, 2, ...) walks a different residue class mod g, and apart from that cycle's own start (again never compared), every member of it is at least i + g > g, so none of them can pull min below g. min therefore settles at exactly gcd(n,k), and the outer test i < min lets i run through 0, 1, ..., gcd(n,k)-1 - one fresh cycle per value, since these are exactly the residue classes' representatives - before stopping exactly at i = gcd(n,k), by which point every index has been covered exactly once.
Cross-check: take n = 5, k = 2 (gcd(5,2) = 1, so all 5 indices form one cycle). Starting at i = 0, j walks 0 -> 2 -> 4 -> 1 -> 3 -> back to 0 under j = (j+k) mod n. The inner-loop stop index is (n+i-k) mod n = (5+0-2) mod 5 = 3, so the walk executes for j = 0, 2, 4, 1 (four moves for a 5-element cycle) and then A[3] = temp closes it. Since gcd(5,2) = 1, the outer loop runs only once (i = 0), then min ends at 1 and the outer test 1 < 1 is false. A second cross-check with n = 6, k = 4 (gcd = 2) confirms the general rule: the outer loop runs for i = 0 and i = 1 (2 = gcd(6,4) cycles), covering all 6 indices exactly once with none left out or double-counted.
Conclusion: the fragment i < min; j != (n+i-k) mod n; A[(j+k) mod n]; temp; i + 1 is the only one consistent with the cycle-following algorithm on every count above.