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
Constraints
- ●
1 <= n == nums1.length <= 100 - ●
1 <= nums1[i] <= 100 - ●
nums1 consists of distinct integers.
Examples
nums1 = [2,3]truenums1 = [4,6]trueAsked At
1 companies| Company | Difficulty | |
|---|---|---|
| Meta | Easy | View all Meta questions → |
How to Think About It
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...
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.
If one of the pair is -1 (unknown), we can always fill it with the appropriate parity to satisfy the constraint. No conflict arises.
The algorithm is simply: scan adjacent pairs. If both known and same parity, return false. Otherwise return true.
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
What Trips People Up in Real Interviews
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.
Solution Code
O(n) timedef 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 TrueFrequently 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.