Move Zeroes — Two Pointer O(n) In-Place Solution [Meta Interview]

Sanjeev SharmaSanjeev Sharma
3 min read

Advertisement

Problem Statement

Given an integer array nums, move all 0s to the end while maintaining the relative order of the non-zero elements. Do it in-place with minimum operations.

Example:

Input:  [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]

Input:  [0, 0, 1]
Output: [1, 0, 0]

Intuition

Write pointer approach: Maintain a write pointer that only advances when we place a non-zero element. After one pass, fill the rest with zeros.

write = 0
for read = 0 to n-1:
    if nums[read] != 0:
        nums[write] = nums[read]
        write++

fill nums[write..n-1] with 0

Why minimal writes? Each non-zero element is written exactly once. The trailing zeros are filled at the end.

Alternative — swap approach: When we find a non-zero, swap it with nums[write]. This avoids the fill step but does more writes if many non-zeros precede a zero.

Solutions in All 5 Languages

C

void moveZeroes(int* nums, int numsSize) {
    int write = 0;
    
    // Copy all non-zeros to front
    for (int read = 0; read < numsSize; read++) {
        if (nums[read] != 0) {
            nums[write++] = nums[read];
        }
    }
    // Fill rest with zeros
    while (write < numsSize) {
        nums[write++] = 0;
    }
}

C++

#include <vector>
using namespace std;

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int write = 0;
        for (int read = 0; read < (int)nums.size(); read++) {
            if (nums[read] != 0) {
                nums[write++] = nums[read];
            }
        }
        fill(nums.begin() + write, nums.end(), 0);
    }
};

// Swap variant (fewer passes but more writes)
class SolutionSwap {
public:
    void moveZeroes(vector<int>& nums) {
        int write = 0;
        for (int i = 0; i < (int)nums.size(); i++) {
            if (nums[i] != 0) {
                swap(nums[write++], nums[i]);
            }
        }
    }
};

Java

class Solution {
    public void moveZeroes(int[] nums) {
        int write = 0;
        for (int num : nums) {
            if (num != 0) nums[write++] = num;
        }
        while (write < nums.length) {
            nums[write++] = 0;
        }
    }
}

JavaScript

function moveZeroes(nums) {
    let write = 0;
    // Move non-zeros forward
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== 0) nums[write++] = nums[i];
    }
    // Fill rest with 0
    while (write < nums.length) nums[write++] = 0;
}

Python

from typing import List

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        write = 0
        for num in nums:
            if num != 0:
                nums[write] = num
                write += 1
        while write < len(nums):
            nums[write] = 0
            write += 1

Complexity Analysis

TimeSpace
OptimalO(n)O(1)

Edge Cases

[0][0]
[1][1]
[0, 0, 0][0, 0, 0]
[1, 2, 3][1, 2, 3]  (no change)

Next: Problem 6 — Plus One

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro