Watch 10 video solutions for Move Zeroes, a easy level problem involving Array, Two Pointers. This walkthrough by NeetCode has 100,922 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
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 <= 104-231 <= nums[i] <= 231 - 1Follow up: Could you minimize the total number of operations done?
Problem Overview: You are given an integer array nums. The task is to move all 0 values to the end of the array while keeping the relative order of non-zero elements unchanged. The operation must be done in-place without creating a new array.
Approach 1: Two-Pointer Approach (O(n) time, O(1) space)
This approach uses two indices to reorganize the array in a single pass. One pointer insertPos tracks where the next non-zero element should be written, while the other pointer scans the array from left to right. Each time you encounter a non-zero value, place it at insertPos and increment the pointer. After the scan completes, fill the remaining positions with zeroes. The key insight is separating the discovery of non-zero values from their final placement, which preserves order naturally. This technique is a classic application of the two pointers pattern on an array. Time complexity is O(n) because each element is visited once, and space complexity is O(1) since all operations happen in-place.
Approach 2: Swap with Two-Pointer Technique (O(n) time, O(1) space)
This variation keeps two pointers: left for the position where the next non-zero element should go, and right for scanning the array. When nums[right] is non-zero, swap it with nums[left], then move both pointers forward. If the current element is zero, only advance right. The swap guarantees that non-zero values gradually shift toward the front while zeroes drift toward the end. Because each element participates in at most one swap, the algorithm still runs in O(n) time with O(1) extra space. This method is often preferred when you want a direct in-place transformation without a second pass to fill zeroes.
Recommended for interviews: Interviewers typically expect the linear O(n) in-place solution using the two-pointer technique. A brute-force approach that repeatedly shifts elements would degrade to O(n^2), which signals inefficient array manipulation. Demonstrating the optimal pointer-based strategy shows you understand how to process arrays with minimal movement and constant space, a common pattern in interview problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-Pointer (Overwrite Non-Zero Elements) | O(n) | O(1) | General case when you want stable ordering and minimal swaps |
| Swap with Two-Pointer Technique | O(n) | O(1) | When an in-place transformation using swaps is simpler to implement |