DSA Tracker

Problem

Easy

Move Zeroes

An easy Two Pointers interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Two Pointers
Sheets
2
Core for
10 roles

The problem

Push every 0 in the array to the end, while the non-zero values keep the order they had among themselves. Do it inside the same array, without making a copy.

Example 1

Input
nums = [4, 0, 7, 0, 2]
Output
[4, 7, 2, 0, 0]

Example 2

Input
nums = [0]
Output
[0]

Constraints

  • 1 <= nums.length <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1

How to think about it

Updated 2026-09-29

Focus entirely on where the non-zero elements belong rather than pushing zeros. A slow pointer records the next vacancy at the front of the array; every time the fast pointer discovers a non-zero, swapping or assigning it forward preserves the relative sequence automatically.

Approaches, worst first

  1. Auxiliary array copy

    time O(n) · space O(n)

    Copy all non-zero elements into a fresh array, then pad the remaining slots with zeros and copy back. Straightforward, but allocates unnecessary O(n) extra memory instead of modifying in place.

  2. Two-pointer write and fill

    time O(n) · space O(1)

    Walk a read pointer across the array, writing every non-zero element to a write index. Once the read pointer finishes, overwrite all remaining indices from write to n-1 with zero.

  3. Two-pointer swapWrite this one

    time O(n) · space O(1)

    Maintain a write pointer at the boundary of settled non-zero elements. Whenever the explorer pointer finds a non-zero value, swap the two positions and advance the write boundary. It clears zeros continuously in a single traversal.

Where people lose marks · 3
  • Unnecessary self-swaps when the array starts with long runs of non-zero elements. Checking `read != write` before swapping avoids pointless writes.
  • Overwriting elements before reading them when using separate write and fill loops. The write pointer must never get ahead of the read pointer.
  • Disrupting relative order by using partition-style swaps from the array tail. A two-pointer strategy from opposite ends places zeros at the back but destroys the original sequence of non-zeros.

The theory behind it

Two Pointers — the ground this problem stands on. All Two Pointers problems

What Two Pointers is

Two pointers is a search coordinated by two fingers placed on different entries of an ordered row, stepping inward toward each other or advancing at different speeds. Instead of scanning with nested loops that re-examine identical pairs, each pointer moves unidirectionally based on comparisons. Because every movement permanently prunes bad candidate combinations, the search inspects the collection in a single joint pass.

When to reach for it

Reach for two pointers when problems require finding index pairs, reversing sequences, trapping rainwater, or filtering duplicates in sorted arrays. When an input is already sorted and an exhaustive search takes quadratic time, opposing pointers meeting in the center often solve it linearly. Fast and slow pointer variations also detect cycles or locate midpoint nodes across linked sequences.

How the pattern works

Initialize pointers at opposing edges for convergent searches, or start both at the beginning to move at different speeds. Under a convergent setup, calculate current pair metrics; if the sum falls short of your target, advance the lower pointer rightward to increase value, and if it exceeds your target, shift the upper pointer leftward to decrease value. Each pointer move throws away only pairs that were already known to be wrong, so the right answer is never missed.

More Two Pointers problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .