EasyArrayMathUpdated Sep 2026

3875. Construct Uniform Parity Array I

Asked at Meta

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Array

Companies

1

Problem

You are given an array nums1 of n distinct integers. You want to build a second array nums2 of the same length n, where every element of nums2 has the same parity — so all elements are odd, or all elements are even.

For each index i, you pick exactly one of two options. Either nums2[i] = nums1[i], or nums2[i] = nums1[i] - nums1[j] for some other index j where j != i. You may use the two options in any order, and different indices may use different options.

Return true if such an array nums2 can be built, otherwise return false.

The key fact: subtracting two numbers gives an odd result only when the two numbers have different parity (one even, one odd). If they have the same parity, the difference is even. So the only way to change an element's parity is to subtract a number of the opposite parity.

All elements must share one parity

nums12301Start with nums1 = [2, 3]: one even value and one oddvalue.nums2-1301Subtract 3 from 2 to get -1, and copy 3. Both are odd,so the answer is true.
We may copy a value or subtract another value, but in the end every element of nums2 must be odd, or every element must be even.

Constraints

  • ●1 <= n == nums1.length <= 100
  • ●1 <= nums1[i] <= 100
  • ●nums1 consists of distinct integers.

Examples

Example 1
Inputnums1 = [2,3]
Outputtrue
WhyUse the first option for index 1: nums2[1] = 3, which is odd. For index 0, subtract the other value: 2 - 3 = -1, which is also odd. Both elements are odd, so it works.
Example 2
Inputnums1 = [4,6]
Outputtrue
WhyBoth numbers are already even, so just copy them: nums2 = [4, 6]. All elements are even.

Asked At

1 companies
CompanyDifficulty
MetaEasyView all Meta questions →

How to Think About It

1

Key insight: for adjacent elements to sum to an odd number, they must have different parity (one even, one odd). So the array must alternate parity: even-odd-even-odd... or odd-even-odd-even...

2

Check all adjacent pairs of known values: if both are known and have the same parity (both even or both odd), their sum is even, so it is impossible -> return false.

3

If one of the pair is -1 (unknown), we can always fill it with the appropriate parity to satisfy the constraint. No conflict arises.

4

The algorithm is simply: scan adjacent pairs. If both known and same parity, return false. Otherwise return true.

5

Edge cases: all -1s (always true), single element (always true), two adjacent known with same parity (false).

Optimal Approach

Step 1: For each adjacent pair (i, i+1) in nums:

  • If both are known (not -1) and have the same parity (both even or both odd): return false.
    Step 2: No conflicts found -> return true.

Walkthrough for nums=[1,-1,1,-1,1]:

  • (1,-1): one unknown, skip.
  • (-1,1): one unknown, skip.
  • (1,-1): one unknown, skip.
  • (-1,1): one unknown, skip.
  • No conflicts. Return true.

Walkthrough for nums=[2,0,2,-1]:

  • (2,0): both even. Conflict! Return false.

Time: O(n) for one scan. Space: O(1).

Count even and odd values

count even values = ecount odd values = oif e == 0 or o == 0: trueif e == 1 and o == 1: trueelse: falseTwo values of the same parity can never be fixed, soonly the all-same-parity and one-of-each cases work.even - even = evenodd - odd = eveneven - odd = oddodd - even = odd
The answer depends only on how many values of each parity exist, not on their order.
ComplexityTime O(n)Space O(1)We only need to look at the parities of the values, which we can count in a single pass with a couple of counters.

What Trips People Up in Real Interviews

1

Overthinking the fill strategy. The problem only asks if it is POSSIBLE, not to construct the array. Just check for conflicts.

2

Checking non-adjacent pairs. Only consecutive elements matter. nums[0] and nums[2] can have the same parity without issue.

3

Confusing odd sum with same parity. Odd sum means different parity. Same parity gives even sum.

4

Missing that -1 elements are wildcards. They can be any integer, so they always adapt to satisfy the constraint with their neighbors.

5

Trying to actually fill the array. The problem is a feasibility check (boolean return), not a construction task.

Solution Code

O(n) time
def isPossible(nums):
    for i in range(len(nums) - 1):
        if nums[i] != -1 and nums[i+1] != -1 and (nums[i] + nums[i+1]) % 2 == 0:
            return False
    return True

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Construct Uniform Parity Array I problem?

Given an array nums of length n where some values are -1 (placeholders), determine if it is possible to replace all -1s with any integers so that every pair of adjacent elements has an odd sum (different parity). Return true if possible, false otherwise.

How do you solve Construct Uniform Parity Array I?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Construct Uniform Parity Array I?

Construct Uniform Parity Array I is asked at Meta. It is a easy difficulty problem.

What are common mistakes on Construct Uniform Parity Array I?
  • Overthinking the fill strategy. The problem only asks if it is POSSIBLE, not to construct the array. Just check for conflicts.
  • Checking non-adjacent pairs. Only consecutive elements matter. nums[0] and nums[2] can have the same parity without issue.
  • Confusing odd sum with same parity. Odd sum means different parity. Same parity gives even sum.
  • Missing that -1 elements are wildcards. They can be any integer, so they always adapt to satisfy the constraint with their neighbors.
  • Trying to actually fill the array. The problem is a feasibility check (boolean return), not a construction task.