Construct Uniform Parity Array I (LeetCode 3875): Solution and Explanation

Easy
Array
Math

Given distinct integers nums1, build nums2 where each slot either keeps nums1[i] or becomes nums1[i] - nums1[j] for another index j. Decide whether nums2 can be made all odd or all even.

Intuition

Only parity matters, so reduce every number to odd or even. Subtraction follows simple rules: odd minus odd is even, even minus even is even, and even minus odd or odd minus even is odd. In other words, subtracting an odd number flips parity and subtracting an even number keeps it.

If every element is even, there is nothing to do. Keep every value and nums2 is all even.

If at least one element is odd, aim for an all-odd nums2. The odd elements keep their own value. Every even element subtracts that one odd element, and even minus odd is odd. The odd element is never subtracted from itself, because it keeps its value, so the rule j != i is respected.

Both cases succeed, so the answer is always true. The code still finds the pivot and checks each slot, which mirrors the construction and makes the argument visible.

Approach: Odd pivot

  1. Scan nums1 for any odd element and call it odd.
  2. If there is none, every value is even already, so return true.
  3. Otherwise each odd value keeps itself and each even value x becomes x - odd, which is odd. Confirm that no slot fails and return true.

Step-by-step example

nums1 = [2,3]
  1. nums1[0] = 2 is even, keep looking.
  2. nums1[1] = 3 is odd, so the pivot is 3 and the goal is all odd.
  3. Slot 0: 2 is even, so nums2[0] = 2 - 3 = -1, which is odd.
  4. Slot 1: 3 is odd, so nums2[1] keeps 3.
  5. nums2 = [-1,3] is all odd. Return true.

Code: Python, Java, C++

1class Solution:
2    def uniformArray(self, nums1: List[int]) -> bool:
3        odd = -1
4        for x in nums1:
5            if x % 2 == 1:
6                odd = x
7                break
8        if odd == -1:
9            return True
10        for x in nums1:
11            if x % 2 == 0 and (x - odd) % 2 == 0:
12                return False
13        return True
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One scan finds the pivot and one scan checks each slot.

Space

O(1)

Only the pivot value is stored; nums2 never needs to be materialized.

Other approaches

Return true directly (time O(1), space O(1))

Once the parity argument is clear, the function body can be a single return true. It is correct but hides the reasoning, so interviewers usually want the explanation alongside it.

Brute force over all choices (time O(n^2), space O(1))

For each target parity, check whether every index has some option (keep or subtract any other element) with that parity. It is O(n^2) and confirms the parity argument on small inputs.

Common mistakes

  • Aiming for all even when the array is mixed. A lone odd element can only become even by subtracting another odd element, which may not exist.
  • Using x % 2 == 1 to test oddness on negative numbers in Java, C++, C# or Go, where the remainder can be -1. Here the inputs are positive, but values like x - odd can be negative.
  • Subtracting an element from itself. The odd pivot keeps its own value instead, so j != i always holds.

Frequently asked questions

Is the answer really always true?

Yes. With no odd element the array is already all even. With at least one odd element, odd values stay and even values subtract that odd element to become odd.

Why not aim for all even when the array is mixed?

Every odd element would have to subtract a different odd element. If there is only one odd element, that is impossible, so all odd is the target that always works.

Does it matter that the values are distinct?

Not for the parity argument. Distinctness only guarantees that nums1[i] - nums1[j] is never zero, which would be even; the construction never relies on it.