#3876
Medium Algorithms Construct uniform parity array ii
Array Math
49.5% acceptance
Mar 31, 2026
65
7
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn uniform_array(nums1: Vec<i32>) -> bool {
// For each index i, nums2[i] = nums1[i] or nums1[i] - nums1[j] (>=1, j!=i).
// We want all nums2 same parity.
// nums1[i] - nums1[j] has parity: even-even=even, odd-odd=even, even-odd=odd, odd-even=odd.
// Let E = count of even, O = count of odd in nums1.
// For index i with nums1[i] even:
// - Keep: even
// - Subtract odd: odd (need an odd element, and result >= 1)
// - Subtract even: even (need another even, and result >= 1)
// For index i with nums1[i] odd:
// - Keep: odd
// - Subtract even: odd (need an even element, and result >= 1)
// - Subtract odd: even (need another odd, and result >= 1)
//
// All even target:
// Even elements can stay even (keep or subtract even).
// Odd elements need to become even: subtract another odd. Need O >= 2 and for
// each odd element, there exists another odd to subtract giving >= 1.
// Since elements are distinct, for odd elements sorted, the largest can subtract the smallest.
// Actually we need EACH odd element to have some other odd j with nums1[i]-nums1[j]>=1.
// This means for each odd i, exists odd j!=i with nums1[j] < nums1[i].
// So the smallest odd element cannot do this unless O==0.
// Wait, but we can also subtract any nums1[j], not just odd ones for making it even.
// odd - odd = even. So for an odd element to produce even, it must subtract another odd.
// The smallest odd element has no smaller odd to subtract, so it can't become even.
// Unless n >= 2 and we're creative... but the smallest odd can't subtract a larger odd (result < 0).
// So all-even is possible only if O == 0, OR O >= 2 and the smallest odd element can subtract some odd.
// The smallest odd can only keep (odd) or subtract to get even (impossible). So all-even requires O == 0.
// Wait, re-read: nums2[i] = nums1[i] - nums1[j] >= 1. The smallest odd, say val=1, can't subtract any odd >= 1 (would get <= 0).
// But if smallest odd is 3, and there's odd 1... wait elements are distinct.
// If O >= 2: sort odd elements. Smallest odd: can subtract a smaller odd? Only if there's a smaller odd. The very smallest has none.
// So all-even impossible unless O == 0.
//
// All odd target:
// Odd elements can stay odd (keep or subtract even).
// Even elements need to become odd: subtract an odd element (even - odd = odd), need result >= 1.
// For each even element, need some odd j with nums1[j] < nums1[i] (so result >= 1).
// The smallest even element needs an odd element strictly less than it.
// If O == 0, no odd to subtract, so even elements can't become odd. All-odd impossible.
// If O >= 1: each even element needs an odd element strictly less. The smallest even...
// Actually we just need EXISTS some odd j with nums1[j] < nums1[i] for each even i.
// min_odd = minimum odd element. Each even element needs min_odd < nums1[i], i.e., nums1[i] > min_odd.
// Also, odd elements: keep (odd), or subtract even (odd-even=odd, need result>=1, exists even < odd_val).
// Actually odd elements can just keep their value (odd). So odd elements are fine.
// Even elements: need min_odd < even_val for each even element.
// If the smallest even > min_odd, all evens can subtract min_odd to get odd.
//
// Summary:
// all-even possible iff O == 0 (all already even)
// all-odd possible iff E == 0 OR (O >= 1 AND min(even elements) > min(odd elements))
let n = nums1.len();
let mut min_odd = i32::MAX;
let mut min_even = i32::MAX;
let mut o_count = 0;
let mut e_count = 0;
for &x in &nums1 {
if x % 2 == 0 {
e_count += 1;
min_even = min_even.min(x);
} else {
o_count += 1;
min_odd = min_odd.min(x);
}
}
// All even: only if no odd elements
if o_count == 0 {
return true;
}
// All odd: if no even elements, trivially true
if e_count == 0 {
return true;
}
// All odd with mix: need min_even > min_odd
if o_count >= 1 && min_even > min_odd {
return true;
}
false
}
}