Home
DSA Patterns
Two Pointers
Move Zeroes
Move Zeroes (LeetCode 283)
Move Zeroes (LeetCode 283): shift every 0 in the integer array nums to the back, in place, keeping the non-zero values in their original order.
Examples
Non-zero elements are 1, 3, 12. They stay in that order. The 0s go to the end.
Problem Understanding
We need to push all 0s to the right side of the array.
Constraints:
- In-place: You cannot copy the valid numbers to a new array. You must do it inside
nums. - Relative Order: If
1comes before3originally, it must stay that way. - Minimize Operations: Ideally O(n) time.
Algorithm Strategy
We can use the Two Pointers technique to partition the array.
leftpointer: Tracks the position where the next non-zero element should go.rightpointer: Scans the array to find non-zero elements.
Logic:
- Iterate through the array with
right. - If
nums[right]is non-zero, we want to place it atnums[left]. - Swap
nums[left]andnums[right]. Thenleft++. - If
nums[right]is zero, just continue scanning (right++).
Interactive Visualization
L WRITES · R READS
WRITE AT 0
L
0
1
2
3
4
Write L
0
Read R
:
nums[R]
:
left marks where the next non-zero value goes; it starts at 0
Next
right reads nums[0] = 0
Array
Watch how L waits for a non-zero value, and R finds it.
Dry Run: nums = [0, 1, 0, 3, 12]
| L (Write) | R (Read/Val) | Action | Array State |
|---|---|---|---|
| 0 | 0 (0) | Ignore 0. R++ | [0, 1, 0, 3, 12] |
| 0 | 1 (1) | Swap nums[L], nums[R]. L++ |
[1, 0, 0, 3, 12] |
| 1 | 2 (0) | Ignore 0. R++ | [1, 0, 0, 3, 12] |
| 1 | 3 (3) | Swap nums[L], nums[R]. L++ |
[1, 3, 0, 0, 12] |
| 2 | 4 (12) | Swap nums[L], nums[R]. L++ |
[1, 3, 12, 0, 0] |
Edge Cases & Common Mistakes
Edge Cases:
- Empty Array: Check
length == 0. - Single Element: No changes needed.
- All Zeroes: Pointers scan through, no swaps.
- No Zeroes:
LandRmove together, swapping element with itself (harmless).
Common Mistakes:
- Creating a copy: Violates space constraint.
- Overwriting without Swap: If you just write non-zeroes to
left, you must remember to fill the rest with zeroes at the end. (Swap method handles this automatically).
Solution
Move Zeroes Code
1var moveZeroes = function(nums) {2 let left = 0;3 for (let right = 0; right < nums.length; right++) {4 if (nums[right] !== 0) {5 [nums[left], nums[right]] = [nums[right], nums[left]];6 left++;7 }8 }9};
Move Zeroes in Python, Java, C++, TypeScript and C
Move Zeroes in Python
def moveZeroes(nums: List[int]) -> None:
left = 0
for right in range(len(nums)):
if nums[right] != 0:
nums[left], nums[right] = nums[right], nums[left]
left += 1Move Zeroes in Java
class Solution {
public void moveZeroes(int[] nums) {
int left = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] != 0) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
}
}
}
}Move Zeroes in C++
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int left = 0;
for (int right = 0; right < nums.size(); right++) {
if (nums[right] != 0) {
swap(nums[left], nums[right]);
left++;
}
}
}
};Move Zeroes in TypeScript
function moveZeroes(nums: number[]): void {
let left = 0;
for (let right = 0; right < nums.length; right++) {
if (nums[right] !== 0) {
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
}
}
}Move Zeroes in C
void moveZeroes(int* nums, int numsSize) {
int left = 0;
for (int right = 0; right < numsSize; right++) {
if (nums[right] != 0) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
}
}
}More practice: DSA interview questions with answers · patterns cheat sheet
