GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 2.15

Array rotation by three reversals

Algorithms · Correctness, Invariants & Design · Full GATE CSE 2000 paper

GATE 2000, Q2.15 2 marks · MCQ
Report issue

Correctness, Invariants & Design

Question 2.15, GATE 2000: For array s[1…n] and 1≤k≤n, execute reverse(s,1,k),reverse(s,k+1,n),reverse(s,1,n). Does this rotate left by k,leave unchanged,reverse everything,or none?
Question 2.15, GATE 2000: For array s[1…n] and 1≤k≤n, execute reverse(s,1,k),reverse(s,k+1,n),reverse(s,1,n). Does this rotate left by k,leave unchanged,reverse everything,or none?
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step 1: Split the original array into consecutive blocks A=s[1…k] and B=s[k+1…n], so it is AB. The first two operations produce AR BR.

Step 2: Reversing the whole concatenation reverses block order and each block: (AR BR)R=BA. This moves the first k entries to the end while preserving order within each block.

Thus A, a left rotation by k. For k=n, the second range is empty and must be treated as a no-op; the full rotation leaves the array unchanged.

Original paper, page 5