An array A consists of n integers in locations A[0], A[1] ....A[n-1]. It is…

2018

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 alt the variables are suitably declared.

C++
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 = __________
  1. A.

    i > min; j!= (n+i)mod n; A[j + k]; temp; i + 1 ;

  2. B.

    i < min; j!= (n+i)mod n; A[j + k]; temp; i + 1;

  3. C.

    i > min; j!= (n+i+k)mod n; A[(j + k)]; temp; i + 1;

  4. D.

    i < min; j!= (n+i-k)mod n; A[(j + k)mod n]; temp; i + 1;

Attempted by 88 students.

Show answer & explanation

Correct answer: D

This problem requires completing a cyclic array shift algorithm known as the Juggling Algorithm. The outer loop iterates through starting positions (i < n). Inside, elements are shifted forward by k using modulo arithmetic: A[j] = A[(j + k) % n]. The inner loop continues until the cycle closes, identified by checking if j reaches the target position (j != (n + i - k) % n). Finally, the stored temp value is placed at A[(n + i - k) % n], and i updates to min + 1.

Explore the full course: Isro