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.
For example:
Input: nums = [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]
Input: nums = [0]
Output: [0]
Constraints:
1 <= nums.length <= 10^4
-2^31 <= nums[i] <= 2^31 - 1
Could you minimize the total number of operations done?
When 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:
The brute force method to move zeroes involves exhaustively checking each position in the list. It aims to create a new list where all the non-zero values appear first, followed by all the zeroes.
Here's how the algorithm would work step-by-step:
def move_zeroes_brute_force(original_list):
new_list = []
for element in original_list:
# Add non-zero elements to the new list.
if element != 0:
new_list.append(element)
# Add the zero elements to the new list
for element in original_list:
if element == 0:
new_list.append(element)
# Modify the original list in-place
for index in range(len(original_list)):
original_list[index] = new_list[index]
# The original list is now modified in-place
return original_list
The goal is to shift all the zeroes to the end while keeping the other numbers in the same order. We achieve this by focusing on the non-zero numbers and moving them to the front.
Here's how the algorithm would work step-by-step:
def move_zeroes(nums):
next_non_zero_position = 0
# Iterate through the array to find non-zero elements.
for current_index in range(len(nums)):
if nums[current_index] != 0:
# Place the non-zero element at the next available position.
nums[next_non_zero_position], nums[current_index] = nums[current_index], nums[next_non_zero_position]
# Increment the index for the next non-zero position.
next_non_zero_position += 1
Case | How to Handle |
---|---|
Null or empty input array | Return immediately; in-place modification is trivial on an empty array. |
Array with one element | Do nothing; the array is already trivially satisfying the condition. |
Array with all zeroes | The 'insertion pointer' will stay at index 0, correctly placing all zeros at the end without changing the relative order. |
Array with no zeroes | The 'insertion pointer' will increment to the end, leaving the array unchanged. |
Array with leading zeros | The insertion pointer will move to the location of first non-zero and correctly swaps all non-zero elements to the front. |
Array with trailing zeroes | The insertion pointer correctly points and swaps all non-zero elements to the front preserving original non-zero ordering. |
Large array with many zeroes scattered throughout | The algorithm uses a single pass, making it efficient even for large arrays. |
Array containing negative numbers along with zeroes | The algorithm works correctly because it only distinguishes between zero and non-zero elements, irrespective of sign. |