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);- A.
Rotates s left by k positions
- B.
Leaves s unchanged
- C.
Reverses all elements of s
- 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.