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 != i, such that nums1[i] - nums1[j] >= 1Return true if it is possible to construct such an array, otherwise return false.
Example 1:
Input: nums1 = [1,4,7]
Output: true
Explanation:
nums2[0] = nums1[0] = 1.nums2[1] = nums1[1] - nums1[0] = 4 - 1 = 3.nums2[2] = nums1[2] = 7.nums2 = [1, 3, 7], and all elements are odd. Thus, the answer is true.Example 2:
Input: nums1 = [2,3]
Output: false
Explanation:
It is not possible to construct nums2 such that all elements have the same parity. Thus, the answer is false.
Example 3:
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 <= 1051 <= nums1[i] <= 109nums1 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 we can make all elements in the array have the same parity (all even or all odd) using either the element itself or the difference between the element and a smaller element in the array. If the minimum element is odd, every even number can subtract this odd minimum to become odd, meaning all elements can be made odd. Conversely, if the minimum element is even, it can never become odd or change its parity, so the only possibility is making all elements even, which requires every element to already be even.
Here's how the algorithm would work step-by-step:
def construct_uniform_parity_array(nums1):
# Find the minimum element in nums1
min_val = min(nums1)
# If the minimum element is odd, we can always make all elements odd
if min_val % 2 == 1:
return True
# Otherwise, check if all elements can be even
# If the minimum is even, we cannot subtract any smaller element from it
# so any odd element cannot be converted to even without an odd element smaller than it
return all(val % 2 == 0 for val in nums1)
To construct an array of uniform parity, we can aim for either all odd or all even numbers. If the smallest element is odd, any even number larger than it can be subtracted by this minimum odd number to become odd, meaning all elements can be made odd. If the smallest element is even, it cannot be subtracted by any smaller positive element, so its parity must remain even, which forces every number in the array to be even without any valid transformation to flip parity.
Here's how the algorithm would work step-by-step:
def can_construct_uniform_parity_array(nums1):
# Find the smallest value among all elements in the array
min_val = min(nums1)
# If the minimum value is odd, all elements can be made odd
if min_val % 2 == 1:
return True
# If the minimum value is even, check if all elements are even
for value in nums1:
if value % 2 != 0:
return False
# All elements were already even, so an even array is possible
return True
| Case | How to Handle |
|---|---|
| Single element array | Return true immediately since an array of size one already has a uniform parity. |
| Array with all even or all odd elements | Return true since the elements already share the same uniform parity without needing any transformations. |
| Array containing negative numbers and zeros | The parity logic relies on modulo operations which correctly determine even and odd properties regardless of the sign. |
| Extremely large inputs up to maximum constraints | An O(N) linear scan approach processes the array efficiently without exceeding time or memory limits. |
| Arrays where transformation rules cannot achieve uniform parity | The algorithm evaluates the mathematical invariants of the operations and returns false when the target parity state is unreachable. |
| Maximum integer boundary values | Language-specific integer types handle standard arithmetic safely without causing unexpected overflows during parity checks. |