DSA · Reimagined
Arrays medium time O(n) space O(1)

Rotate an Array

To left-rotate by k, reverse the first k elements, reverse the rest, then reverse the whole array. Three in-place reversals rotate with no extra memory.

1
0
2
1
3
2
4
3
5
4
6
5
7
6

Left-rotate by k = 3 using three reversals.

1 / 11

Practice

Machine twin: /dsa-viz-v2/rotate-array.json — the full deterministic run as structured data.