Code Logo

Move Zeroes

Published at05 Jan 2026
Easy 35 views
Like18

This problem is about cleaning up the array without changing the order of the important numbers. In Move Zeroes, you want every 0 to end up at the back while all the non-zero values stay in the same relative order.

That detail matters a lot. You are not sorting the array, and you are not allowed to shuffle the non-zero values however you want. You only want to push the zeroes to the end and keep everything else lined up the same way as before.

For example, if the input is nums = [0,1,0,3,12], the answer is [1,3,12,0,0]. The non-zero values 1, 3, and 12 stay in the same order, and both zeroes move to the back. Another example is nums = [0,0,1], which gives [1,0,0]. The 1 moves forward, and the zeroes slide behind it.

So the goal is to rearrange the same array so that every non-zero value stays in order at the front, and every zero is moved to the remaining positions at the end.

Example Input & Output

Example 1
Input
nums = [0,1,0,3,12]
Output
[1,3,12,0,0]
Explanation

Example with input: nums = [0,1,0,3,12]

Example 2
Input
nums = [0,0,1]
Output
[1,0,0]
Explanation

The 1 stays first among non-zero values, and both zeroes move to the end.

Example 3
Input
nums = [2,1]
Output
[2,1]
Explanation

Example with input: nums = [2,1]

Algorithm Flow

Recommendation Algorithm Flow for Move Zeroes
Recommendation Algorithm Flow for Move Zeroes

Solution Approach

A good method for this problem is the two-pointer approach, sometimes called a write pointer method. The idea is to scan through the array once, copy each non-zero number to the next open position, and then fill the rest of the array with zeroes.

The benefit of this approach is that it works in place, so we do not need to build a separate array. It also keeps the non-zero numbers in the same order and runs in O(n) time, which makes it both efficient and easy to follow.

We start by creating a pointer that tells us where the next non-zero value should be written:

let write = 0;

This means write always points to the next position we want to fill with a non-zero number.

Next, we go through the array one value at a time:

for (let i = 0; i < nums.length; i++) {
  if (nums[i] !== 0) {
    nums[write] = nums[i];
    write++;
  }
}

Whenever we see a non-zero value, we copy it into nums[write] and move write forward. This packs all non-zero values at the front of the array in their original order.

After that first pass, every position before write is correct. The remaining positions should all become zero:

while (write < nums.length) {
  nums[write] = 0;
  write++;
}

This fills the rest of the array with zeroes, which finishes the rearrangement.

So the full idea is simple: first move all non-zero values forward, then put zeroes in the leftover spaces. That is why this approach works so nicely here. The time complexity is O(n), and the extra space complexity is O(1).

Best Answers

java
import java.util.*;
class Solution {
    public int[] move_zeroes(int[] nums) {
        int[] res = new int[nums.length];
        int idx = 0;
        for (int x : nums) if (x != 0) res[idx++] = x;
        return res;
    }
}