#283 Move Zeroes
the problem
In plain words
Move all 0s to the end of the array while keeping the relative order of the non-zero elements. Do it in place.
in nums = [0,1,0,3,12]
out [1,3,12,0,0]
in nums = [0]
out [0]
- 1 ≤ n ≤ 10⁴
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternTwo pointers (read / write)
Pack non-zeros to the front with a write pointer; swapping (instead of copying) makes the zeros end up at the back automatically.
solutions
From brute force to optimal
Copy non-zeros into a fresh array (rest stays 0), then copy back.
- copy = new int[n].
- Append non-zeros.
- Copy back.
Two passes.
The extra array — violates "in place".
class Solution { public void moveZeroes(int[] nums) { int[] copy = new int[nums.length]; // zeros by default int k = 0; for (int x : nums) if (x != 0) copy[k++] = x; System.arraycopy(copy, 0, nums, 0, nums.length); }}When nums[read] ≠ 0, swap it with nums[write] and advance write.
- write = 0.
- For each read: if non-zero, swap(read, write), write++.
One pass.
In place.
class Solution { public void moveZeroes(int[] nums) { int write = 0; for (int read = 0; read < nums.length; read++) { if (nums[read] != 0) { int tmp = nums[write]; // swap keeps zeros flowing right nums[write] = nums[read]; nums[read] = tmp; write++; } } }}Dry run
in the room
How to say it in the interview
talk track
- Confirm: in place, and the non-zero values must keep their order. Negatives count as non-zero.
- Brute: copy the non-zeros into a new array and copy back — O(n) time, but O(n) extra space.
- Optimal: a write pointer for the next non-zero slot; swap each non-zero into it, and the zeros drift to the back.
- State it: O(n) time, O(1) space. Test [0], an array with no zeros, and [0, 0, 1].
traps
- Sorting the array — that breaks the original order of the non-zeros.
- Removing zeros with
ArrayList.removeand adding them at the end: O(n²) and not in place. - Copying non-zeros forward but forgetting to fill the tail with zeros afterwards.
what they ask next
Follow-ups
Can you reduce the number of writes?
Copy non-zeros forward (nums[write++] = nums[read]), then fill the rest with 0. Or keep the swap but skip it when read == write. Both stay O(n) time, O(1) space.
Move all copies of some value val instead of 0?
Same loop: swap forward every element that isn't val. It's LC 27 Remove Element, but keeping order.
before you submit
Edge cases to test
- Only zeros, or no zeros at all: the array must stay as it is.
- A single element [0] → [0].
- Negative numbers are non-zero too: [0, -2] → [-2, 0].
- Order matters: [4, 0, -2, 0, 7] → [4, -2, 7, 0, 0], not sorted.
practise next
Similar problems
- #27Remove ElementEasyin Two Pointers
- #26Remove Duplicates from Sorted ArrayEasyin Two Pointers
- #75Sort ColorsMediumin Two Pointers