Watch 9 video solutions for Construct Uniform Parity Array II, a medium level problem involving Array, Math. This walkthrough by Developer Coder has 616 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.Problem Overview: You need to construct an array that satisfies a strict parity rule: the parity of each element must match the parity of its index. In practice, even indices should hold even numbers and odd indices should hold odd numbers (or vice‑versa depending on indexing rules). The task is not about searching or sorting; it is about constructing a valid arrangement that satisfies the constraint.
Approach 1: Brain Teaser Construction (O(n) time, O(1) space)
The key observation is that parity only has two states: even and odd. If the array length allows an equal distribution of both parities, you can construct the array directly by placing numbers with matching parity at the correct indices. Iterate through indices from 0 to n-1. For every even index, place the next available even number; for every odd index, place the next available odd number. Because each placement is deterministic, you avoid backtracking or searching.
This works because parity alignment guarantees the condition automatically. There is no need for additional data structures like a hash table or auxiliary arrays. Each step performs constant work while traversing the array exactly once, giving O(n) time complexity and O(1) extra space.
The approach is essentially a constructive greedy strategy. Instead of checking all permutations, you build a valid configuration directly. Problems like this often appear under arrays or greedy categories where recognizing the structure eliminates brute force.
If constraints require specific value ranges (for example using numbers 1..n), maintain two counters: one for the next odd value and one for the next even value. Move them forward as you fill the array. Since each number is placed exactly once, the solution stays linear and memory‑efficient.
Recommended for interviews: The direct construction method is what interviewers expect. Explaining why parity constraints allow deterministic placement shows strong problem decomposition skills. A brute‑force permutation check demonstrates understanding but quickly becomes impractical due to factorial growth, while the constructive approach shows you can recognize patterns and reduce the problem to a simple linear pass.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Permutation | O(n!) | O(n) | Conceptual understanding or verifying small cases |
| Brain Teaser Construction | O(n) | O(1) | Best general solution when constructing array with parity constraints |