edited by
5,180 views
1 votes
1 votes

An array $A$ consists of $n$ integers in locations $A[0], A[1], \ldots 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 bellow. Complete the algorithm by filling in the blanks.

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=________;
    
}

$\begin{array}{lllll}
\text{a.}&i>\min;&  j !=(n+1) \mod n;& A[j+k]& \text{temp};& i+1; \\     
\text{b.}&i<\min; &  j !=(n+i)\mod n;& A[j+k]& \text{temp};&  i+1; \\
\text{c.}&i>\min;& j !=(n+i+k) \mod n; & A[j+k] & \text{temp};& i+1; \\      
\text{d.}&i<\min; & j !=(n+i-k) \mod n;& A[(j+k) \mod n]& \text{temp};& i+1;
\end{array}$

edited by

1 Answer

Best answer
13 votes
13 votes
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 $ i<min$ because if it is $i>min$ 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) mod n$

That means whenever $j$ becomes equal to $(n+i)mod n$  then control goes out of the while loop.

Now $(n+i)mod n = 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.
edited by
Answer:

Related questions

1 votes
1 votes
2 answers
1
5 votes
5 votes
2 answers
2
Arjun asked Apr 22, 2018
4,623 views
Consider the following C code segmentint f(int x) { if(x<1) return 1; else return (if(x-1)+g(x)); } int g(int x) { if(x<2) return 2; else return (if(x-1)+g(x/2)); }Of the...
3 votes
3 votes
1 answer
3
Arjun asked Apr 22, 2018
5,078 views
The following paradigm can be used to find the solution of the problem in minimum time:Given a set of non-negative integer and a value $K$, determine if there is a subset...
4 votes
4 votes
2 answers
4
Arjun asked Apr 22, 2018
3,900 views
Which of the following is application of Breath First Search on the graph?Finding diameter of the graphFinding bipartite graphBoth (a) and (b)None of the above