DSA · Two pointers (in-place) · #24
Move Zeroes
Module 27 · difficulty 1/5·⏱ 30:00starts on first keystroke
Given an integer array `nums`, move all `0`'s to the end while keeping the relative order of the non-zero elements. You must do this in-place, mutating `nums` directly (return value is ignored).
Examples
nums = [0,1,0,3,12]→[1,3,12,0,0]nums = [0]→[0]nums = [1,2,3]→[1,2,3]
Stuck? Reveal an animated walkthrough of the approach.
Constraints
- · 1 <= nums.length <= 10^4
- · -2^31 <= nums[i] <= 2^31 - 1
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.