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: Given an integer array, move all 0 values to the end while keeping the relative order of non‑zero elements unchanged. The operation must be done in-place without creating another array.
This problem is a classic in-place array manipulation task. The challenge is preserving order while minimizing writes. Most optimal solutions rely on array traversal combined with the two pointers technique to track where the next non-zero element should go.
Approach 1: Two-Pointer Approach (Time: O(n), Space: O(1))
Maintain a pointer insertPos that tracks the position where the next non-zero element should be written. Iterate through the array from left to right. Whenever you encounter a non-zero value, place it at nums[insertPos] and increment insertPos. After processing all elements, fill the remaining positions in the array with zeros. This approach works because all non-zero elements are compacted to the front in their original order during the first pass.
The key insight is separating the read pointer from the write pointer. You scan the array once to collect non-zero values and then overwrite the tail with zeros. The algorithm performs at most n writes and keeps the relative order intact. Because it uses only two integer pointers and modifies the input array directly, the space complexity stays constant.
Approach 2: Swap with Two-Pointer Technique (Time: O(n), Space: O(1))
This version keeps two pointers: left pointing to the position where the next non-zero element should be placed, and right scanning the array. When nums[right] is non-zero, swap it with nums[left] and move both pointers forward. If the element is zero, only advance right. Over time, all non-zero values get swapped toward the front and zeros drift toward the back.
The advantage of the swap technique is simplicity. Each non-zero element is moved exactly once to its correct region. However, this version may perform unnecessary swaps when left == right, though the overall complexity remains linear. Many interview candidates prefer this method because the logic mirrors common partition-style algorithms used in array problems.
Recommended for interviews: Interviewers typically expect the two-pointer in-place solution with O(n) time and O(1) space. Showing the compaction idea (writing non-zero elements forward) demonstrates strong understanding of in-place array manipulation. The swap-based two-pointer variant is equally acceptable and often easier to explain during a whiteboard discussion.
This approach uses two pointers: one to track the position for the next non-zero element and the other to iterate through the array. We move all non-zero elements to the beginning of the array using these two pointers and fill the remaining positions with zeroes.
The code defines a function moveZeroes that takes an array nums and its size numsSize. The function maintains a pointer lastNonZeroFoundAt to keep track of the position to place the next non-zero element. As we iterate through nums, we shift non-zero elements to the front and then fill the remaining elements with zeroes.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the length of the array. We make a single pass through the array.
Space Complexity: O(1), as we perform the operation in place.
This method uses a two-pointer technique where we place one pointer at the beginning of the array and the other to iterate through the array. Whenever we encounter a non-zero element, we swap it with the first pointer's position, allowing us to effectively move zeroes to the end by swapping.
This C function moves zeroes to the end by swapping elements within the array. A pointer j is used to track the location for swapping non-zero elements found during iteration.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), single iteration with swaps.
Space Complexity: O(1), in-place swaps.
| Approach | Complexity |
|---|---|
| Two-Pointer Approach | Time Complexity: O(n), where n is the length of the array. We make a single pass through the array. |
| Swap with Two-Pointer Technique | Time Complexity: O(n), single iteration with swaps. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-Pointer Compaction | O(n) | O(1) | General case when minimizing extra memory and preserving order is required |
| Swap with Two-Pointer Technique | O(n) | O(1) | When you want a simpler pointer logic similar to partition-style array algorithms |
Move Zeroes - Leetcode 283 - Python • NeetCode • 100,922 views views
Watch 9 more video solutions →Practice Move Zeroes with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor