In-place array rotation

How do you rotate an array left by kk positions in O(n)O(n) time and O(1)O(1) extra space?

Show hints (2)+
  1. Reversal can be done in place.
  2. Three well-chosen reversals compose into a rotation.

Answer

Reveal answer →

Reverse first kk, reverse the rest, reverse the whole - the triple-reversal trick

Create a free account to see the full worked solution and track your progress.

Interview format: Hedge Fund Quant, Options Trading

Related questions