You are given an array of integers nums
where half of the integers are odd and the other half are even.
Your task is to sort the array such that whenever nums[i]
is odd, i
is odd, and whenever nums[i]
is even, i
is even.
Return any array that satisfies this condition.
For example:
nums = [4, 2, 5, 7]
, a possible output is [4, 5, 2, 7]
. Other valid outputs include [4, 7, 2, 5]
and [2, 5, 4, 7]
. The key is that even numbers are at even indices and odd numbers are at odd indices.nums = [2, 3]
, the output should be [2, 3]
.Constraints:
nums
is between 2 and 2 * 10^4.nums
is even.nums
are even.nums[i]
is between 0 and 1000.Could you describe an algorithm to solve this problem efficiently? What is the time and space complexity of your solution?
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 way to sort an array by parity II is like trying every single possible arrangement. We'll check each arrangement to see if it meets the 'even number in even spot, odd number in odd spot' rule.
Here's how the algorithm would work step-by-step:
import itertools
def sort_array_by_parity_ii_brute_force(numbers):
# Generate all possible permutations of the input array
for permutation in itertools.permutations(numbers):
is_valid_permutation = True
# Check if the current permutation satisfies the parity condition
for index, number in enumerate(permutation):
#Verify if the number and index have the same parity
if (index % 2 == 0 and number % 2 != 0) or \
(index % 2 != 0 and number % 2 == 0):
is_valid_permutation = False
break
#If valid return converted to list
if is_valid_permutation:
return list(permutation)
return [] # Return empty list if no valid permutation is found
The key idea is to directly place numbers into their correct positions based on whether they are even or odd. We use two pointers to efficiently find numbers that are out of place and swap them to fix the array without needing to sort it fully.
Here's how the algorithm would work step-by-step:
def sort_array_by_parity_ii(numbers):
even_index = 0
odd_index = 1
# Iterate until either pointer reaches the end of the array.
while even_index < len(numbers) and odd_index < len(numbers):
# Find an even number at an odd index.
while even_index < len(numbers) and numbers[even_index] % 2 == 0:
even_index += 2
# Find an odd number at an even index.
while odd_index < len(numbers) and numbers[odd_index] % 2 != 0:
odd_index += 2
# If both pointers are still within bounds, swap the elements.
if even_index < len(numbers) and odd_index < len(numbers):
# Swap to put numbers in correct parity positions.
numbers[even_index], numbers[odd_index] = numbers[odd_index], numbers[even_index]
even_index += 2
odd_index += 2
return numbers
Case | How to Handle |
---|---|
Null or empty input array | Return an empty array or throw an IllegalArgumentException, depending on requirements. |
Input array with an odd number of elements | Throw an IllegalArgumentException as the problem requires an even number of elements. |
Input array where the number of even elements doesn't equal the number of odd elements | Throw an IllegalArgumentException since a valid solution is impossible. |
Array contains only even numbers | The algorithm should still correctly place even numbers in even indices and odd numbers in odd indices assuming appropriate pre-validation for balanced parity. |
Array contains only odd numbers | The algorithm should still correctly place even numbers in even indices and odd numbers in odd indices assuming appropriate pre-validation for balanced parity. |
Array with a very large number of elements (potential performance bottleneck) | The chosen algorithm (e.g., two-pointer approach) should have a time complexity of O(n) to handle large inputs efficiently. |
Integer overflow when dealing with extremely large numbers | The parity check (num % 2) should work correctly regardless of the size of the integer within the language's limits. |
Input array contains negative numbers | The parity check (num % 2) should function correctly for negative numbers, distinguishing between even and odd. |