Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.
Return any array that satisfies this condition.
Example 1:
Input: nums = [3,1,2,4] Output: [2,4,3,1] Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Example 2:
Input: nums = [0] Output: [0]
Constraints:
1 <= nums.length <= 50000 <= nums[i] <= 5000When 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 mixed pile of items where some are of one type and the others are of a different type, and you want to group all of one specific type together at the beginning. The brute force strategy involves meticulously searching through your collection to find what you are looking for and organizing the items by completely re-examining the collection multiple times.
Here's how the algorithm would work step-by-step:
def sort_array_by_parity(input_numbers):
workspace_array = []
# We scan first to isolate all even numbers at the front of our new workspace
for current_number in input_numbers:
if current_number % 2 == 0:
workspace_array.append(current_number)
# We scan a second time to append remaining odd numbers after the evens
for current_number in input_numbers:
if current_number % 2 != 0:
workspace_array.append(current_number)
# We return the fully reorganized workspace containing both grouped types
return workspace_arrayThe optimal approach uses two searchers starting from opposite ends of the line to organize the items efficiently. By moving inward and swapping items when they are in the wrong places, the collection is sorted in a single pass.
Here's how the algorithm would work step-by-step:
def sort_array_by_parity(input_array):
left_searcher = 0
right_searcher = len(input_array) - 1
# Traverse inward until pointers meet to partition evens and odds efficiently.
while left_searcher < right_searcher:
while left_searcher < right_searcher and input_array[left_searcher] % 2 == 0:
left_searcher += 1
while left_searcher < right_searcher and input_array[right_searcher] % 2 == 1:
right_searcher -= 1
# Swap mispositioned elements so evens move left and odds move right.
if left_searcher < right_searcher:
input_array[left_searcher], input_array[right_searcher] = input_array[right_searcher], input_array[left_searcher]
left_searcher += 1
right_searcher -= 1
return input_array| Case | How to Handle |
|---|---|
| Empty array or null input | Return the array as-is since no rearrangement is necessary. |
| Array with a single element | Return the array unchanged because a single element is already sorted by parity. |
| Array with all even numbers | Pointers traverse the array without swapping, leaving all elements in their original positions. |
| Array with all odd numbers | The left pointer immediately finds an odd number and stays put while the right pointer scans to the beginning, resulting in no changes. |
| Array with alternating even and odd numbers | The two-pointer swap approach efficiently moves evens to the front and odds to the back in a single pass. |
| Array containing negative numbers and zero | Parity is determined by modulo two, so negative even numbers and zero correctly route to the beginning of the array. |
| Maximum constraint array size up to the problem limit | An in-place two-pointer approach scales linearly with time complexity O(N) and space complexity O(1). |
| Multiple valid solutions possible | The problem accepts any ordering of evens followed by odds, so the relative order of elements does not need to be preserved. |