Skip to main content

Construct Uniform Parity Array II - Solution & Explanation

MediumArrayMath7 min readAsked at: Amdocs
Practice this problem

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 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:

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 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:

  • Set nums2[0] = nums1[0] = 4.
  • Set nums2[1] = nums1[1] = 6.
  • nums2 = [4, 6], and all elements are even. Thus, the answer is true.

 

Constraints:

  • 1 <= n == nums1.length <= 105
  • 1 <= nums1[i] <= 109
  • nums1 consists 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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force PermutationO(n!)O(n)Conceptual understanding or verifying small cases
Brain Teaser ConstructionO(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 CoderDeveloper Coder670 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Construct Uniform Parity Array II easy or hard?
Construct Uniform Parity Array II is generally considered a medium difficulty problem. The logic becomes simple once you recognize that parity constraints allow deterministic placement. Without that insight, many candidates initially attempt unnecessary brute‑force or rearrangement strategies.
Construct Uniform Parity Array II Python/Java solution
The implementation in Python, Java, C++, Go, or TypeScript follows the same pattern: iterate through indices and assign values whose parity matches the index. Maintain two variables tracking the next available even and odd numbers and increment them after placement.
How to solve Construct Uniform Parity Array II in O(n)?
Maintain two pointers or counters: one generating even numbers and another generating odd numbers. Traverse the array indices sequentially. If the index is even, assign the next even value; if the index is odd, assign the next odd value. This single pass builds a valid array in linear time.
What is the best approach for Construct Uniform Parity Array II?
The optimal solution is a direct construction (brain teaser) approach. Iterate through the array and place numbers whose parity matches the index parity. Even indices receive even values and odd indices receive odd values. This guarantees correctness in O(n) time and O(1) extra space.
Is Construct Uniform Parity Array II asked at Google/Amazon/Meta?
Parity‑based array construction and index constraints appear frequently in interviews at large tech companies. Variants of this pattern show up in problems involving array rearrangement, greedy construction, or parity constraints. Practicing these helps with common interview patterns.
What data structure is used in Construct Uniform Parity Array II?
The problem primarily uses a simple array with constant counters for even and odd numbers. No advanced structures such as heaps, trees, or hash maps are required. The solution relies on parity checks and sequential array construction.
What is the time complexity of Construct Uniform Parity Array II?
The optimal construction algorithm runs in O(n) time because the array is filled in a single pass. Each index receives a value exactly once, and only constant operations occur per step. Space complexity is O(1) since only a few counters for even and odd numbers are needed.

Ready to solve this problem?

Practice Construct Uniform Parity Array II with our built-in code editor and test cases.

Practice on FleetCode