Mid/SeniorArrayMath

Construct Uniform Parity Array II

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] >= 1

Return true if it is possible to construct such an array, otherwise return false.

Example 1
Inputnums1 = [1,4,7]
Outputtrue
By choosing values [1, 3, 7] for nums2, all elements are odd, so the answer is true.
Example 2
Inputnums1 = [2,3]
Outputfalse
It is not possible to construct nums2 such that all elements have the same parity, so the answer is false.

Constraints

  • 1 <= n == nums1.length <= 10^5
  • 1 <= nums1[i] <= 10^9
  • nums1 consists of distinct integers.
</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate