Given an integer array nums
, move all the even integers to the beginning of the array, followed by all the odd integers. Return any array that satisfies this condition.
For example:
Input: nums = [3, 1, 2, 4]
Output: [2, 4, 3, 1]
(Other valid outputs: [4, 2, 3, 1]
, [2, 4, 1, 3]
, and [4, 2, 1, 3]
).Input: nums = [0]
Output: [0]
Clarifications:
Write a function to solve this problem with optimal time and space complexity.
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 approach to sorting by parity means we explore all possible arrangements of the numbers. We check each arrangement to see if it satisfies the condition that even numbers come before odd numbers. If it does, we keep it; otherwise, we discard it.
Here's how the algorithm would work step-by-step:
import itertools
def sort_array_by_parity_brute_force(numbers):
all_permutations = list(itertools.permutations(numbers))
valid_solutions = []
for permutation in all_permutations:
# Check if the current permutation satisfies the parity condition.
is_valid = True
# Iterate through the permutation and check the parity.
even_numbers_passed = False
for number in permutation:
if number % 2 == 0:
if even_numbers_passed:
is_valid = False
break
else:
# Mark that we have passed even numbers.
even_numbers_passed = True
if is_valid:
# Store the permutation if it's a valid solution.
valid_solutions.append(list(permutation))
# If there are any valid solutions, return the first one.
if valid_solutions:
return valid_solutions[0]
else:
return []
The goal is to rearrange numbers such that all even numbers appear before all odd numbers. The clever trick is to use two pointers, one at the beginning and one at the end, and swap numbers when they're out of order.
Here's how the algorithm would work step-by-step:
def sort_array_by_parity(numbers):
start_index = 0
end_index = len(numbers) - 1
while start_index < end_index:
# Find the first odd number from the left
while start_index < end_index and numbers[start_index] % 2 == 0:
start_index += 1
# Find the first even number from the right
while start_index < end_index and numbers[end_index] % 2 != 0:
end_index -= 1
# Swap if an odd number is on the left and an even on the right
if start_index < end_index:
numbers[start_index], numbers[end_index] = numbers[end_index], numbers[start_index]
start_index += 1
end_index -= 1
return numbers
Case | How to Handle |
---|---|
Null or empty input array | Return the input array immediately if null or empty, as there's nothing to sort. |
Array with one element | Return the single-element array immediately, as it is already sorted by parity. |
Array with all even numbers | The algorithm should correctly place all even numbers at the beginning, resulting in no changes. |
Array with all odd numbers | The algorithm should correctly place all odd numbers at the end, resulting in a complete swap of elements. |
Array with alternating even and odd numbers | The algorithm will perform swaps until all even numbers are at the front and odd numbers are at the back. |
Array with large size (potential performance issue) | Ensure the algorithm used has a time complexity of O(n) or O(n log n) to avoid performance bottlenecks with large arrays; a two-pointer approach is optimal. |
Array with negative even and odd numbers | The parity check should work correctly with negative numbers (e.g., num % 2 == 0 determines evenness). |
Array containing zero values | Zero is considered even, so the algorithm should place zeros towards the beginning of the array. |