Construct an Array with Uniform Parity

Quick Overview

Decide whether distinct positive integers can form an all-odd or all-even array using one allowed choice per position and strictly positive differences.

Construct an Array with Uniform Parity

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Construct an Array with Uniform Parity You are given an array `nums1` of `n` distinct positive integers. Decide whether you can construct an array `nums2` of length `n` whose elements are either all odd or all even. For every index `i`, choose exactly one of these assignments: - Keep the value: `nums2[i] = nums1[i]`. - Choose an index `j != i` and set `nums2[i] = nums1[i] - nums1[j]`, provided that the difference is at least `1`. Every assignment uses the original values in `nums1`. Choosing a value as a subtrahend does not consume or change it, so the same index `j` may be used for several other positions. You cannot apply several subtractions to one position. Return `true` if such an array exists, and `false` otherwise. You do not need to return `nums2`. ### Function `bool uniformArray(vector<int>& nums1)` ### Example 1 **Input:** `nums1 = [9, 14, 5, 8]` **Output:** `true` **Explanation:** Keep `9` and `5`, replace `14` by `14 - 5 = 9`, and replace `8` by `8 - 5 = 3`. The resulting array `[9, 9, 5, 3]` is all odd. Repeated values in the constructed array are allowed. ### Example 2 **Input:** `nums1 = [6, 11, 18]` **Output:** `false` **Explanation:** The value `6` cannot be replaced by a positive difference, so the constructed array would have to be all even. The value `11` cannot become even: keeping it or subtracting `6` gives an odd value, while subtracting `18` is not permitted. ### Constraints - `1 <= n == nums1.length <= 100000` - `1 <= nums1[i] <= 1000000000` - All values in `nums1` are distinct. ### Clarifications - The subtraction result must be strictly positive; zero and negative results are invalid. - Only `nums1` must contain distinct values. The output values may repeat. - A one-element array already has uniform parity. ### What a Strong Answer Covers - Respecting the one-choice-per-position rule and the positivity condition. - Justifying both possible and impossible outcomes. - Handling the full input size without constructing an unnecessary output array.

Overview: Decide whether distinct positive integers can form an all-odd or all-even array using one allowed choice per position and strictly positive differences.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Construct an Array with Uniform Parity

You are given an array nums1 of n distinct positive integers. Decide whether you can construct an array nums2 of length n whose elements are either all odd or all even.

For every index i, choose exactly one of these assignments:

  • Keep the value: nums2[i] = nums1[i] .
  • Choose an index j != i and set nums2[i] = nums1[i] - nums1[j] , provided that the difference is at least 1 .

Every assignment uses the original values in nums1. Choosing a value as a subtrahend does not consume or change it, so the same index j may be used for several other positions. You cannot apply several subtractions to one position.

Return true if such an array exists, and false otherwise. You do not need to return nums2.

Function

bool uniformArray(vector<int>& nums1)

Example 1

Input: nums1 = [9, 14, 5, 8]

Output: true

Explanation: Keep 9 and 5, replace 14 by 14 - 5 = 9, and replace 8 by 8 - 5 = 3. The resulting array [9, 9, 5, 3] is all odd. Repeated values in the constructed array are allowed.

Example 2

Input: nums1 = [6, 11, 18]

Output: false

Explanation: The value 6 cannot be replaced by a positive difference, so the constructed array would have to be all even. The value 11 cannot become even: keeping it or subtracting 6 gives an odd value, while subtracting 18 is not permitted.

Constraints

  • 1 <= n == nums1.length <= 100000
  • 1 <= nums1[i] <= 1000000000
  • All values in nums1 are distinct.

Clarifications

  • The subtraction result must be strictly positive; zero and negative results are invalid.
  • Only nums1 must contain distinct values. The output values may repeat.
  • A one-element array already has uniform parity.

What a Strong Answer Covers Guidance

  • Respecting the one-choice-per-position rule and the positivity condition.
  • Justifying both possible and impossible outcomes.
  • Handling the full input size without constructing an unnecessary output array.