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

2024

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 22 students.

Show answer & explanation

Correct answer: A

Answer: (a)

Effect of the above 3 reversals for any k is equivalent to left rotation of the array of size n by k.

If we rotate an array n times for k = 1 to n, we get the same array back.

Explore the full course: Coding For Placement