Partition Array Into Alternating Parity Groups
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests in-place partitioning by rearranging an integer array so all even-indexed positions hold even numbers and all odd-indexed positions hold odd numbers, without allocating a new array.
Partition Array Into Alternating Parity Groups
You are given a list of integers that is guaranteed to contain exactly as many even numbers as odd numbers. Your task is to rearrange the list in-place so that:
- Every even index (0, 2, 4, …) holds an even number.
- Every odd index (1, 3, 5, …) holds an odd number.
Because multiple valid arrangements exist, the following tie-break rule defines a unique answer:
- Collect all even numbers from the original list, preserving their relative order.
- Collect all odd numbers from the original list, preserving their relative order.
- Interleave them: position 0 ← even[0], position 1 ← odd[0], position 2 ← even[1], position 3 ← odd[1], …
You must perform this rearrangement in-place (modify the input list directly) and also return it.
Constraints
2 ≤ len(nums) ≤ 2000len(nums)is always even.- The list always contains exactly
len(nums) // 2even numbers andlen(nums) // 2odd numbers. -10^6 ≤ nums[i] ≤ 10^60is considered even.- Negative numbers retain their usual parity (e.g.,
-3is odd,-4is even).
Examples
Example 1
Input: partition_alternating_parity([4, 1, 2, 3])
Output: [4, 1, 2, 3]
Example 2
Input: partition_alternating_parity([10, 7, 6, 3, 2, 5])
Output: [10, 7, 6, 3, 2, 5]
Example 3
Input: partition_alternating_parity([0, -3, -4, 9])
Output: [0, -3, -4, 9]
Hints
Hint 1
Try making two passes first: one to collect even-indexed values (the evens in order) and one to collect odd-indexed values (the odds in order), then write them back in-place.
Hint 2
You can use two write-pointers — one stepping by 2 starting at index 0 for even numbers, another stepping by 2 starting at index 1 for odd numbers — and a single read-pass over the original values.
Hint 3
To keep it truly in-place, save the original values with a temporary copy of the array before overwriting, or partition using index arithmetic rather than a new list.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key insight: The tie-break rule fully determines the output — even numbers (in their original relative order) must land on even indices, and odd numbers (in their original relative order) must land on odd indices. This removes all ambiguity.
Steps
- Snapshot the original list into a temporary copy so that in-place writes do not corrupt values we have not yet read.
- Partition the snapshot into two sub-lists in a single linear scan:
evens: all valuesxwherex % 2 == 0, in the order they appeared.odds: all valuesxwherex % 2 != 0, in the order they appeared.
- Interleave back into
numsin-place using a single loop: indexiis even → write fromevens, indexiis odd → write fromodds. - Return
nums(the same list object).
Why it is correct
The input guarantee ensures len(evens) == len(odds) == len(nums) // 2, so there are no out-of-bounds accesses. The interleaving places exactly one even at every even index and one odd at every odd index. By stepping through each sub-list in order, the relative order of both groups is preserved, matching the tie-break rule exactly.
Complexity
| Value | |
|---|---|
| Time | O(n) — two linear passes (partition + interleave) |
| Space | O(n) — the snapshot copy and two sub-lists each of length n/2 |
An O(1)-extra-space variant is possible with a two-pointer swap approach, but it requires care to preserve relative order (which a naïve swap does not). The snapshot approach is straightforward and pedagogically clear for this exercise.
from typing import List
def partition_alternating_parity(nums: List[int]) -> List[int]:
# Snapshot original values to avoid reading already-overwritten slots
original = nums[:]
# Separate evens and odds while preserving relative order
evens = [x for x in original if x % 2 == 0]
odds = [x for x in original if x % 2 != 0]
# Interleave back in-place: even indices ← evens, odd indices ← odds
ei = oi = 0
for i in range(len(nums)):
if i % 2 == 0:
nums[i] = evens[ei]
ei += 1
else:
nums[i] = odds[oi]
oi += 1
return nums