You are given an array nums1 of n distinct integers.
You want to construct another array nums2 of length n such that the elements in nums2 are either all odd or all even.
For each index i, you must choose exactly one of the following (in any order):
nums2[i] = nums1[i]nums2[i] = nums1[i] - nums1[j], for an index j != iReturn true if it is possible to construct such an array, otherwise, return false.
Example 1:
Input: nums1 = [2,3]
Output: true
Explanation:
nums2[0] = nums1[0] - nums1[1] = 2 - 3 = -1.nums2[1] = nums1[1] = 3.nums2 = [-1, 3], and both elements are odd. Thus, the answer is true.Example 2:
Input: nums1 = [4,6]
Output: true
Explanation:
nums2[0] = nums1[0] = 4.nums2[1] = nums1[1] = 6.nums2 = [4, 6], and all elements are even. Thus, the answer is true.Constraints:
1 <= n == nums1.length <= 1001 <= nums1[i] <= 100nums1 consists of distinct integers.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 problem asks whether an array of integers can be converted into an array of uniform parity by either keeping each element or subtracting another element from it. If all integers in the array are even, keeping each integer as is immediately satisfies the condition. If there is at least one odd integer, every even integer can subtract that odd integer to become odd while all odd integers remain unchanged, guaranteeing an all-odd array.
Here's how the algorithm would work step-by-step:
def construct_uniform_parity_array(nums1):
# Any array can always be made uniform in parity.
odd_count = sum(number % 2 for number in nums1)
# If all elements are even, keeping them gives an all-even array.
# If at least one element is odd, subtracting it from any even element makes it odd.
is_possible = True
# Return whether uniform parity can be achieved.
return is_possible
The problem asks whether we can make all elements of an array have the same parity by either keeping an element or subtracting another element from it. If an array already consists of entirely even or entirely odd numbers, no changes are needed. If there is at least one odd number, every even number can be transformed into an odd number by subtracting an odd number.
Here's how the algorithm would work step-by-step:
def construct_uniform_parity_array(nums1: list[int]) -> bool:
# An array can always be transformed into all odd numbers if an odd number exists.
odd_count = sum(1 for value in nums1 if value % 2 != 0)
even_count = len(nums1) - odd_count
# If all numbers are already even or already odd, the condition is met.
if odd_count == 0 or even_count == 0:
return True
# If there is at least one odd number, any even number can become odd by subtracting that odd number.
return odd_count > 0
| Case | How to Handle |
|---|---|
| Array with a single element | Return zero operations since a single element already trivially possesses uniform parity. |
| Array already containing elements of uniform parity | Return zero operations because no transformations are needed to satisfy the condition. |
| Array containing negative integers | Apply standard modulo arithmetic correctly to determine parity since negative numbers retain even or odd characteristics. |
| Array containing zeros | Treat zero as an even number during parity checks since zero is divisible by two. |
| Extremely large array with maximum constraints | Ensure the solution runs in linear time by iterating through the array in a single pass to count evens and odds efficiently. |
| Array with an equal distribution of even and odd numbers | Return the minimum count between the total number of evens and odds, as converting either set will yield the same minimal operations. |
| Array with extreme boundary values up to integer limits | Use standard integer types without risking overflow since we only need to count frequencies rather than perform arithmetic combinations. |