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 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a line of items containing some special empty placeholders and you want to push all the empty placeholders to the very end while keeping the rest of the items in their original relative order. The brute force approach relies on creating a completely separate workspace to rebuild the line from scratch.
Here's how the algorithm would work step-by-step:
def move_zeroes(number_list):
# We need a separate workspace to temporarily hold all the non-zero elements in order
workspace_list = []
for current_index in range(len(number_list)):
if number_list[current_index] != 0:
workspace_list.append(number_list[current_index])
# Counting missing spots allows us to pad the end with the correct number of zeroes
zero_count = len(number_list) - len(workspace_list)
for padding_index in range(zero_count):
workspace_list.append(0)
# Copying back is required to modify the original array in place as expected
for original_index in range(len(number_list)):
number_list[original_index] = workspace_list[original_index]The optimal strategy for this problem is like organizing a line of people by shuffling all the non-zero items forward while keeping them in their original order. By scanning through the line and keeping track of where the next non-zero item belongs, we can neatly group all the zeroes at the very end in a single pass.
Here's how the algorithm would work step-by-step:
def move_zeroes(items_collection):
next_available_spot = 0
# Shift all non-zero elements forward to preserve their relative order in the array.
for current_index in range(len(items_collection)):
if items_collection[current_index] != 0:
items_collection[next_available_spot] = items_collection[current_index]
next_available_spot += 1
# Fill the remaining trailing positions with zeroes after all non-zeroes are placed.
for trailing_index in range(next_available_spot, len(items_collection)):
items_collection[trailing_index] = 0
# The array is now modified in place with all zeroes pushed to the back.| Case | How to Handle |
|---|---|
| Empty array or single element array | The two-pointer approach safely skips the loop or performs no swaps, leaving the array unchanged as required. |
| Array already contains no zeroes | The fast pointer will iterate to the end and continuously overwrite elements with themselves without causing issues. |
| Array consists entirely of zeroes | The slow pointer remains at the beginning while the fast pointer traverses the entire array, leaving all zeroes intact. |
| All non-zero elements are at the end of the array | The pointers will efficiently swap the leading zeroes with the trailing non-zero elements to push all zeroes back. |
| Array contains alternating zeroes and non-zeroes | The partition pattern correctly gathers all non-zero elements to the front while preserving their relative order. |
| Array contains negative numbers | Since the condition checks strictly for non-zero values, negative numbers are treated identically to positive non-zero integers. |
| Extremely large array scaling | The linear time O(n) pass with constant O(1) extra space ensures optimal performance and prevents memory limit issues. |
| Trailing zeroes already at the end of the array | The algorithm minimizes unnecessary writes by only swapping or overwriting when a non-zero element precedes a zero. |