Arrays — Visual Learning

Rotate Array —
Three reversals. Zero extra space.

Rotating an array by k positions seems like it needs extra space — but the reversal trick does it in O(1). Reverse the whole array, reverse the first k elements, reverse the rest. Three reversals and you're done. Watch each reversal transform the array on LearnBug and the trick becomes impossible to forget.

O(n)Time complexity
O(1)Space complexity
PythonLanguage

What is Array Rotation?

Rotating an array right by k means every element shifts k positions to the right, and elements that fall off the end wrap around to the beginning. [1, 2, 3, 4, 5] rotated right by 2 becomes [4, 5, 1, 2, 3]. The brute force approach shifts one position at a time in O(n·k). The reversal trick does it in O(n) with O(1) space.

Why visualization changes everything

The reversal trick feels counterintuitive — why does reversing three times produce the correct rotation? On LearnBug each reversal highlights the swapped pairs and you watch the array transform step by step. After seeing the three stages animate, the "why" clicks and you won't need to re-derive it in an interview.

YouTube — Rotate Array Explained Visually
📺 Drop your YouTube embed here
LearnBug — three reversals animating
🖼 Add a LearnBug screenshot here
Walkthrough

Rotate right by k=2 using three reversals

Array: [1, 2, 3, 4, 5], k = 2. Expected output: [4, 5, 1, 2, 3]

1

Step 1 — Reverse the entire array

[1, 2, 3, 4, 5][5, 4, 3, 2, 1]. Two pointers swap from both ends inward.

5
4
3
2
1

After reverse all: [5, 4, 3, 2, 1]

2

Step 2 — Reverse first k=2 elements

[5, 4][4, 5]. Only the first two positions swap.

4
5
3
2
1

After reverse first k: [4, 5, 3, 2, 1]

3

Step 3 — Reverse remaining n-k elements

[3, 2, 1][1, 2, 3]. The last three positions swap inward.

4
5
1
2
3

Final result: [4, 5, 1, 2, 3]

4

Always normalise k first

If k = 7 on an array of length 5, rotating 7 times = rotating 2 times (7 % 5 = 2). Always apply k = k % n before rotating. If k = 0 or k = n, the array is unchanged.

1
2
3
4
5

k = 7 % 5 = 2 → same result as above

Python Code

Three approaches — O(1) to O(n) space

PythonReversal Trick — O(1) space (optimal)
def rotate(nums, k):
    n = len(nums)
    k = k % n              # handle k > n

    def reverse(left, right):
        while left < right:
            nums[left], nums[right] = nums[right], nums[left]
            left += 1; right -= 1

    reverse(0, n - 1)       # step 1: reverse all
    reverse(0, k - 1)       # step 2: reverse first k
    reverse(k, n - 1)       # step 3: reverse rest
PythonSlicing — O(n) space (simpler but uses extra memory)
def rotate_slice(nums, k):
    n     = len(nums)
    k     = k % n
    nums[:]  = nums[-k:] + nums[:-k]   # last k + first n-k

Time & Space Complexity

Time — ReversalO(n)Three reversal passes — each element swapped at most once
Space — ReversalO(1)Only two pointer variables — no extra array allocated
Time — SlicingO(n)Creates a new array of size n under the hood
Space — SlicingO(n)Extra array — fine for Python, not acceptable in-place constraint

Frequently asked questions

Why does reversing three times produce the correct rotation?

When you reverse the whole array, the last k elements (which should become the first k after rotation) end up at the front — but reversed. Reversing just that first k segment fixes their internal order. Similarly, the first n-k elements end up at the back reversed — reversing that segment fixes their order. Three reversals undo each other's damage while achieving the rotation.

What is the difference between rotating left and rotating right?

Rotating right by k is the same as rotating left by n-k. To rotate left by k using the reversal trick: reverse first k, reverse last n-k, then reverse all. The order of the three reversal steps changes but the idea is identical.

Why do we compute k = k % n?

Rotating an array of length n by n positions returns it to its original state. So rotating by 7 is the same as rotating by 7 % 5 = 2 (for n=5). Without this step, if k ≥ n, the slice indices become wrong and you'd get an incorrect result or index out of bounds error.

What is the brute force approach and why is it worse?

Brute force rotates one position at a time: save the last element, shift every element one position right, place the saved element at index 0. Repeat k times. That's O(n·k) — for k ≈ n this becomes O(n²). The reversal trick is always O(n) regardless of k.

What is the LeetCode problem for array rotation?

LeetCode 189 — Rotate Array. The problem asks for an in-place solution, which is the reversal trick. The follow-up asks you to achieve O(1) extra space — that's exactly what the reversal approach does. Also related: LC 796 (Rotate String) applies the same circular shift concept to strings.

Watch the three reversals transform your array

Paste your rotate problem into LearnBug and see each reversal swap pairs step by step.

Open Playground →