Move Zeroes — Solution in Python, Java & C++
Move Zeroes solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem, asked at Zomato.
Move Zeroes is an easy coding problem on arrays and two pointers, asked in Zomato interviews.
- Difficulty: Easy
- Topics: Arrays, Two Pointers
- Asked at: Zomato
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer array nums, move all 0s to the end while keeping the relative order of the non-zero elements, then return the array.
Do this in place with O(1) extra memory, then return nums.
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 <= 30-100 <= nums[i] <= 100
Hints for Move Zeroes
Hint 1
Keep a write pointer for where the next non-zero belongs.
Hint 2
After copying every non-zero forward, fill the rest with zeros.
How to solve Move Zeroes
Keep a write pointer marking where the next non-zero value belongs. Sweep the array, and every time you meet a non-zero, copy it to the write position and advance. Once the sweep ends, everything from the write pointer onwards must be zeros.
Approach
- Set
write = 0. - Scan the array with a read index.
- On a non-zero value, store it at
nums[write]and incrementwrite. - After the scan, fill positions
write .. n-1with0. - Return the array.
Why it works
The non-zero values are copied in the order they were met, so their relative order is preserved — which is the part of the problem a naive swap-everything approach gets wrong. The write pointer never overtakes the read pointer, so no unread value is ever clobbered. Counting is implicit: the number of trailing slots is exactly the number of zeros removed.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Order among the non-zero values must be preserved, so swapping with the array's end is wrong.
- Do not forget the second pass that writes the trailing zeros — otherwise stale values remain.
- An array of all zeros or no zeros must both work; the two-phase structure handles them without special cases.
Reference solution
Python
from typing import List
def moveZeroes(nums: List[int]) -> List[int]:
w = 0
for x in nums:
if x != 0:
nums[w] = x
w += 1
for i in range(w, len(nums)):
nums[i] = 0
return numsJava
public static int[] moveZeroes(int[] nums) {
int write = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[write] = nums[i];
write++;
}
}
for (int i = write; i < nums.length; i++) nums[i] = 0;
return nums;
}C++
vector<int> moveZeroes(vector<int>& nums) {
int write = 0;
for (int i = 0; i < (int) nums.size(); i++) {
if (nums[i] != 0) {
nums[write] = nums[i];
write++;
}
}
for (int i = write; i < (int) nums.size(); i++) nums[i] = 0;
return nums;
}JavaScript
var moveZeroes = function(nums) {
let w = 0;
for (const x of nums) {
if (x !== 0) nums[w++] = x;
}
while (w < nums.length) nums[w++] = 0;
return nums;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Two Pointers Technique