Open In App
Related Articles

ISRO | ISRO CS 2018 | Question 24

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 = __________

                    
(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;



Answer: (D)

Explanation:

In the five blanks given in the question, the last two blanks must be temp and i+1 because all the given options for the fourth and fifth blanks have temp and i+1. Now, for the first blank, it must be imin then the control goes out of the while loop in the initial case when i=0 and min=n So, the first blank is i < min which implies either option (B) or option (D) is correct. Assume option (B) is correct then in the bracket of while we have j!=(n+i)modn That means whenever j becomes equal to (n+i)modn then control goes out of the while loop. Now (n+i)modn=i and j is always equal to i because in line 3 of the code we are assigning the value of i to j. So, if option (B) is true control never enters the second while loop but it has to enter the second while loop to shift the nos. K places left. Hence, option (D) is correct.



Quiz of this Question
Please comment below if you find anything wrong in the above post

Last Updated : 07 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads