AlgoMaster Logo

Move Zeroes

Last Updated: February 6, 2026

Ashish

Ashish Pratap Singh

easy

Problem Description

Solve it on LeetCode

Understanding the Problem

At first glance, this problem seems trivial: just find the zeros and move them to the end. But there are several constraints that make it interesting:

  1. In-place requirement: We cannot create a new array and copy elements over. We must modify the original array.
  2. Maintain relative order: The non-zero elements must appear in the same order they originally had. We cannot just swap zeros with non-zeros from the end, as that would disrupt the order.
  3. Minimize operations (follow-up): The optimal solution should avoid unnecessary writes to the array.

Let's visualize what we need to achieve:

The non-zero elements [1, 3, 12] maintain their relative order. The zeros have been pushed to the end.

A key observation is that we need to "shift" all non-zero elements to the front of the array. Once all non-zeros are in their correct positions at the front, whatever remains at the back will naturally be zeros.

Approaches

1. Brute Force with Extra Space

Intuition

The most intuitive approach is to create a temporary array, copy all non-zero elements first, then fill the remaining positions with zeros. While this violates the in-place constraint, it helps us understand the core logic before optimizing.

Algorithm

  1. Create a temporary array of the same size
  2. Iterate through the original array and copy all non-zero elements to the temp array
  3. Fill the remaining positions in temp array with zeros
  4. Copy the temp array back to the original array

Code

Example Walkthrough:

Input:

0
0
1
1
2
0
3
3
4
12
nums

After the first pass (collect non-zeroes into result):

0
1
1
3
2
12
3
0
4
0
result

The second pass fills the remaining positions with zero (they may already be zero, but that’s fine):

0
1
1
3
2
12
3
0
4
0
result

Finally, we copy result back into nums:

0
1
1
3
2
12
3
0
4
0
nums

This approach works correctly but uses extra space. Let's improve it.

2. Two-Pass In-Place Solution

Intuition

We can achieve an in-place solution by breaking the problem into two phases:

  1. First pass: Move all non-zero elements to the front, keeping track of where to place the next non-zero element.
  2. Second pass: Fill all remaining positions with zeros.

Think of it like compacting a file system: first, move all the "used" blocks to the front, then mark the rest as "free" (zeros).

Algorithm

  1. Initialize a pointer writePos at 0 to track where the next non-zero element should go
  2. Iterate through the array:
    • When we find a non-zero element, place it at writePos and increment writePos
  3. After the first pass, writePos tells us where the zeros should start
  4. Fill all positions from writePos to the end with zeros

Code:

Example Walkthrough:

Input:

0
0
1
1
2
0
3
3
4
12
nums

After first pass:

0
1
1
3
2
12
3
3
4
12
nums

After second pass:

0
1
1
3
2
12
3
0
4
0
nums

3. Optimal Two-Pointer with Swapping

Intuition

We can fine-tune our previous two-pointer approach by swapping in place.

Maintain two indices:

  • writePos points to the next position where a non-zero should live.
  • readPos scans the array left to right.

Whenever nums[readPos] is non-zero, swap it with nums[writePos] (only if readPos != writePos) and advance writePos. This compacts non-zeros in a single pass and leaves zeros behind naturally.

Code

Example Walkthrough:

0
writePos
0
readPos
1
1
2
0
3
3
4
12
Step 1 / 6