3875. Construct Uniform Parity Array I
EasyView on LeetCode
Problem Overview
Each nums2[i] can stay nums1[i] or become a difference nums1[i] - nums1[j].
Intuition
Each nums2[i] can stay nums1[i] or become a difference nums1[i] - nums1[j]. Parity of a difference follows parity rules: odd minus even and even minus odd are odd. If nums1 is already uniform in parity, copy it. If both parities appear, subtract an element of opposite parity at every index and every value becomes odd.
Algorithm
- 1Check whether all nums1 entries share parity (all even or all odd).
- 2If yes, nums2 = nums1 works.
- 3If not, find any odd value and any even value in nums1.
- 4For each i, set nums2[i] = nums1[i] minus a chosen value of opposite parity.
- 5All results are odd, so return true. Under the problem constraints the answer is always true.
Example Walkthrough
Input: nums1 = [2, 3]
- 1.Mixed parity: use odd minus even.
- 2.nums2[0] = 2 - 3 = -1 (odd). nums2[1] = 3 (odd).
- 3.Both odd, so construction succeeds.
Output: true
Common Pitfalls
- •The answer is always true for n >= 1 with distinct values; do not overthink with search.
- •Difference parity depends on the two operands, not on their order beyond sign.
- •You may keep some indices as nums1[i] and use differences on others.
- •Distinctness guarantees two different indices for the subtraction when n > 1.
3875.cs
C#
// Approach: If nums1 is all even or all odd, use nums2 = nums1. Otherwise pick one
// odd and one even value; odd minus even is odd, so every index can be set to
// nums1[i] - nums1[j] with opposite parity and all entries become odd. Always
// possible under the given constraints.
// Complexity: O(1) time and O(1) extra space.
public class Solution
{
public bool UniformArray(int[] nums1)
{
return true;
}
}
Was this solution helpful?
Related Problems
- 66. Plus One(Easy)
- 179. Largest Number(Medium)
- 189. Rotate Array(Medium)
- 368. Largest Divisible Subset(Medium)
- 477. Total Hamming Distance(Medium)
- 486. Predict the Winner(Medium)