Construct Uniform Parity Array II - Solution & Explanation
Problem Statement
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 indexj != i, such thatnums1[i] - nums1[j] >= 1
Return true if it is possible to construct such an array, otherwise return false.
Example 1:
Input: nums1 = [1,4,7]
Output: true
Explanation:
- Set
nums2[0] = nums1[0] = 1. - Set
nums2[1] = nums1[1] - nums1[0] = 4 - 1 = 3. - Set
nums2[2] = nums1[2] = 7. nums2 = [1, 3, 7], and all elements are odd. Thus, the answer istrue.
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:
- Set
nums2[0] = nums1[0] = 4. - Set
nums2[1] = nums1[1] = 6. nums2 = [4, 6], and all elements are even. Thus, the answer istrue.
Constraints:
1 <= n == nums1.length <= 1051 <= nums1[i] <= 109nums1consists of distinct integers.
Approach Overview
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.
Solution
If all elements in nums1 are either all odd or all even, we can directly set nums2 equal to nums1, which satisfies the condition.
If nums1 contains both odd and even numbers, we need to find the minimum odd number mn, and check whether there exists an even number x in nums1 such that x < mn. If such an even number exists, we cannot construct a valid nums2, so we return false; otherwise we return true.
The time complexity is O(n), where n is the length of the array nums1. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| 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 |
Video Solution
Construct Uniform Parity Array II | LeetCode 3876 | Weekly Contest 494 | Java | Developer Coder • Developer Coder • 670 views views
Watch 6 more video solutions →Frequently Asked Questions
Is Construct Uniform Parity Array II easy or hard?
Construct Uniform Parity Array II Python/Java solution
How to solve Construct Uniform Parity Array II in O(n)?
What is the best approach for Construct Uniform Parity Array II?
Is Construct Uniform Parity Array II asked at Google/Amazon/Meta?
What data structure is used in Construct Uniform Parity Array II?
What is the time complexity of Construct Uniform Parity Array II?
Ready to solve this problem?
Practice Construct Uniform Parity Array II with our built-in code editor and test cases.
Practice on FleetCode