Given an array of integers nums of even length n, determine whether the array can be partitioned into n/2 pairs such that the sum of each pair is an odd number. Every element belongs to exactly one pair.
Return True if such a partition exists, and False otherwise.
Note: the sum of two integers is odd if and only if one integer is even and the other is odd.
Function Signature
def can_split_into_odd_sum_pairs(nums: list[int]) -> bool:
Rules
-
A pair can use any two elements of the array. The elements of a pair do not have to be adjacent.
-
Zero is even. Negative numbers follow the usual parity rule, so
-3
is odd and
-8
is even.
Constraints
-
2 <= len(nums) <= 10^5
-
len(nums)
is even
-
-10^9 <= nums[i] <= 10^9
Examples
Input: nums = [1, 2, 3, 4]
Output: True
The pairs (1, 2) and (3, 4) have sums 3 and 7, both odd. The array has two odd numbers and two even numbers.
Input: nums = [1, 3, 5, 7]
Output: False
All four numbers are odd, and the sum of two odd numbers is even, so every possible pair fails.
Input: nums = [-3, 0, -8, 5]
Output: True
The pairs (-3, 0) and (5, -8) both sum to -3, which is odd.