Suppose you are given an array s[1..n] and a procedure reverse(s, i, j) which…

2014

Suppose you are given an array s[1..n] and a procedure reverse(s, i, j) which reverses the order of elements in a between positions i and j (both inclusive). What does the following sequence do, where 1 <= k < n:

reverse(s, 1, k);
reverse(s, k + 1, n);
reverse(s, 1, n);

  1. A.

    Rotates s left by k positions

  2. B.

    Leaves s unchanged

  3. C.

    Reverses all elements of s

  4. D.

    None of the above

Attempted by 149 students.

Show answer & explanation

Correct answer: A

Let the first k elements be A and the remaining n-k elements be B. The first reversal makes A^R B. The second reversal makes A^R B^R. Reversing the whole array gives B A, which is the original array rotated left by k positions.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir