Taro Logo

Construct Uniform Parity Array II

Medium
Asked by:
Amdocs
0 views
Topics:
ArraysGreedy Algorithms

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.

Example 1:

Input: nums1 = [1,4,7]

Output: true

Explanation:​​​​​​​​​​​​​​

  • Set nums2[0] = nums1[0] = 1.
  • Set nums2[1] = nums1[1] - nums1[0] = 4 - 1 = 3.
  • Set nums2[2] = nums1[2] = 7.
  • nums2 = [1, 3, 7], and all elements are odd. Thus, the answer is true.

Example 2:

Input: nums1 = [2,3]

Output: false

Explanation:

It is not possible to construct nums2 such that all elements have the same parity. Thus, the answer is false.

Example 3:

Input: nums1 = [4,6]

Output: true

Explanation:

  • Set nums2[0] = nums1[0] = 4.
  • Set nums2[1] = nums1[1] = 6.
  • nums2 = [4, 6], and all elements are even. Thus, the answer is true.

Constraints:

  • 1 <= n == nums1.length <= 105
  • 1 <= nums1[i] <= 109
  • nums1 consists of distinct integers.

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. What are the exact transformation rules and allowable operations for modifying the array elements?
  2. Can the input array contain negative numbers or zeros, and how do they affect the parity calculations?
  3. What should be returned if the array is empty or contains only a single element?
  4. Are we allowed to modify the input array in place, or should we return a new boolean result without mutating the original data?
  5. Is there any restriction on the number of operations we can perform, or can we apply the transformations indefinitely until the goal is met?

Brute Force Solution

Approach

The problem asks whether we can make all elements in the array have the same parity (all even or all odd) using either the element itself or the difference between the element and a smaller element in the array. If the minimum element is odd, every even number can subtract this odd minimum to become odd, meaning all elements can be made odd. Conversely, if the minimum element is even, it can never become odd or change its parity, so the only possibility is making all elements even, which requires every element to already be even.

Here's how the algorithm would work step-by-step:

  1. Find the minimum value among all distinct integers in nums1.
  2. Check if this minimum value is odd.
  3. If the minimum is odd, return true because all elements can be made odd by subtracting this minimum from even numbers.
  4. If the minimum is even, check if all numbers in nums1 are already even.
  5. Return true if all numbers are even, otherwise return false.

Code Implementation

def construct_uniform_parity_array(nums1):
    # Find the minimum element in nums1
    min_val = min(nums1)
    
    # If the minimum element is odd, we can always make all elements odd
    if min_val % 2 == 1:
        return True
    
    # Otherwise, check if all elements can be even
    # If the minimum is even, we cannot subtract any smaller element from it
    # so any odd element cannot be converted to even without an odd element smaller than it
    return all(val % 2 == 0 for val in nums1)

Big(O) Analysis

Time Complexity
O(n) – Finding the minimum value and checking the parity of each element both require a single pass through the array of length n.
Space Complexity
O(1) – Only a few scalar variables are used to store the minimum value and evaluate conditions, requiring constant auxiliary space.

Optimal Solution

Approach

To construct an array of uniform parity, we can aim for either all odd or all even numbers. If the smallest element is odd, any even number larger than it can be subtracted by this minimum odd number to become odd, meaning all elements can be made odd. If the smallest element is even, it cannot be subtracted by any smaller positive element, so its parity must remain even, which forces every number in the array to be even without any valid transformation to flip parity.

Here's how the algorithm would work step-by-step:

  1. Identify the minimum element across the given array.
  2. Check if the minimum element is odd.
  3. Return true immediately if the minimum element is odd, as all even elements can be reduced by this minimum to become odd.
  4. If the minimum element is even, iterate through the array to check if any element is odd.
  5. Return false if any odd element is found, because it cannot be turned even without a smaller odd element.
  6. Return true if all elements in the array are even.

Code Implementation

def can_construct_uniform_parity_array(nums1):
    # Find the smallest value among all elements in the array
    min_val = min(nums1)
    # If the minimum value is odd, all elements can be made odd
    if min_val % 2 == 1:
        return True
    # If the minimum value is even, check if all elements are even
    for value in nums1:
        if value % 2 != 0:
            return False
    # All elements were already even, so an even array is possible
    return True

Big(O) Analysis

Time Complexity
O(n) – We find the minimum value in O(n) time and perform at most one linear scan over the array of size n to check the parities.
Space Complexity
O(1) – Only a few auxiliary variables are used to keep track of the minimum value and loop through the list, requiring constant extra space.

Edge Cases

Single element array
How to Handle:
Return true immediately since an array of size one already has a uniform parity.
Array with all even or all odd elements
How to Handle:
Return true since the elements already share the same uniform parity without needing any transformations.
Array containing negative numbers and zeros
How to Handle:
The parity logic relies on modulo operations which correctly determine even and odd properties regardless of the sign.
Extremely large inputs up to maximum constraints
How to Handle:
An O(N) linear scan approach processes the array efficiently without exceeding time or memory limits.
Arrays where transformation rules cannot achieve uniform parity
How to Handle:
The algorithm evaluates the mathematical invariants of the operations and returns false when the target parity state is unreachable.
Maximum integer boundary values
How to Handle:
Language-specific integer types handle standard arithmetic safely without causing unexpected overflows during parity checks.