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

PYQ GATE 1994

An array A contains 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 variables are suitably declared.

min:=n;
i:=0;
while ______ do
begin
    temp:=A[i];
    j:=i;
    while ______ do
    begin
        A[j]:=______;
        j:=(j+K) mod n;
        if j<min then
            min:=j;
    end;
    A[(n+i-K) mod n]:=______;
    i:=______;
end;

Attempted by 2 students.

Sign up free to check your answer

Sign up free

Explore the full course: Data Structures

Loading lesson…