Move Zeroes

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Move Zeroes

Approach

Use a slow pointer to track where the next non-zero element should go. The fast pointer scans the array and whenever it finds a non-zero element, swap it with the element at the slow pointer position. This partitions the array into non-zeroes followed by zeroes.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Two Pointers (Same Direction)

Why It Works

Swapping non-zero elements to the slow pointer position preserves their relative order while naturally pushing zeroes to the end.

Updated Feb 2026