DSA Tracker

Easy

Move Zeroes

An easy Two Pointers problem included in Apna College, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Two Pointers
Sheets
2
Core for
10 roles
Platform
LeetCode

The problem

Given an array of integers, move all zeros to the end while maintaining the relative order of the non-zero elements.

Example 1

Input
nums = [0, 1, 0, 3, 12]
Output
[1, 3, 12, 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-09

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.

What each operation costs

OperationTime
converging scan across sorted collectionO(n)
slow and fast cycle traversalO(n)
auxiliary memory overhead for pointer markersO(1)
What usually goes wrong with Two Pointers
  • Applying convergent opposite-end pointers to an unsorted collection without realizing that sorting was a necessary precondition for direction-based pruning.
  • Allowing pointers to cross past one another or crash into identical indices when matching duplicate values, skipping the termination condition.
  • Forgetting to advance pointers past repeated values during duplicate-skipping loops, creating unexpected infinite iterations on matching elements.

Which roles need this problem

Two Pointers is a core topic for these 10 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 15 more roles, including SDE / Backend Engineer, ML Engineer, Graphics Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Two Pointers problems

Problem set and role mapping as of .