Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions problems/3875-construct-uniform-parity-array-i/analysis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 3875. Construct Uniform Parity Array I

[LeetCode Link](https://leetcode.com/problems/construct-uniform-parity-array-i/)

Difficulty: Easy
Topics: Array, Math
Acceptance Rate: 80.6%

## Hints

### Hint 1

The actual values of `nums1` never matter — only whether each one is odd or even. Rewrite the problem in your head with `nums1` replaced by a string of `E`s and `O`s and ask what each allowed move does to a single letter. Anything about the numbers being distinct, or bounded by 100, is scenery.

### Hint 2

Parity arithmetic has only four rules: `E - E = E`, `O - O = E`, `E - O = O`, `O - E = O`. So the move `nums2[i] = nums1[i] - nums1[j]` is really "flip index `i`'s parity if `nums1[j]` is odd, leave it alone if `nums1[j]` is even." Keeping `nums2[i] = nums1[i]` is "leave it alone." Now ask: what do you need available in the array in order to flip a chosen index?

### Hint 3

You need exactly one thing: a single odd element somewhere. Nothing in the problem says an index `j` may be used only once, so one odd value at index `k` can be subtracted from *every* other index. That means whenever the array is mixed (at least one odd and at least one even), you can keep every odd as-is and subtract `nums1[k]` from every even, turning them all odd. And if the array is *not* mixed, it is already uniform, so you keep everything. There is no arrangement of parities left over — the answer is always `true`.

## Approach

Work entirely in parity space. Let `O` be the set of indices holding odd values and `E` the set holding even values.

**Case 1: the array is already uniform** (`O` is empty, or `E` is empty). Choose the first option, `nums2[i] = nums1[i]`, for every index. `nums2` is a copy of `nums1`, so it is all even or all odd respectively. Done.

**Case 2: the array is mixed** — both `O` and `E` are non-empty. Pick any single index `k ∈ O`. Build `nums2` like this:

- for `i ∈ O`: take `nums2[i] = nums1[i]`, which is odd;
- for `i ∈ E`: take `nums2[i] = nums1[i] - nums1[k]`, which is `even - odd = odd`.

The second choice is legal for every `i ∈ E`, because `i` holds an even value and `k` holds an odd one, so `i != k` is automatic — we never need the forbidden `j == i`. And critically, the problem places no budget on `k`: the same index may serve as the subtrahend for all of `E`. Every entry of `nums2` is odd, so the array is uniform.

Those two cases cover every possible input, so the answer is unconditionally `true`.

A worked example, `nums1 = [4, 7, 10, 12]`: parities are `E O E E`, which is mixed, so take `k = 1` (value `7`). Then `nums2 = [4-7, 7, 10-7, 12-7] = [-3, 7, 3, 5]` — all odd. Note how index 1 was reused three times.

It is worth being explicit about why the *other* target is not needed. Making everything **even** is the harder goal: an odd index `i` can only be fixed by subtracting some *other* odd value, so a lone odd element would be stuck. That is exactly the case that tempts people into answering `false`. But the all-odd target rescues it: with one odd element present, every even element has something to borrow, and the lone odd simply keeps itself. Having two escape routes and only needing one is what collapses this problem.

Both of the problem's examples return `true`, and no `false` example is given — a fair signal that you are meant to discover there is no failing input.

The honest note on difficulty: the *code* here is a one-liner, but the proof is the whole exercise, and "prove no counterexample exists" is a genuinely different skill from "find the algorithm." Being suspicious of a trivial answer is healthy; the fix is to write out the case analysis until you are convinced, rather than trusting the hunch either way.

## Complexity Analysis

Time Complexity: O(1) — the answer does not depend on reading the input at all. Any implementation that scans the array to classify parities is O(n), which is also perfectly acceptable.
Space Complexity: O(1) — `nums2` never has to be materialized, only shown to exist.

## Edge Cases

- **`n == 1`**: the second option is unusable, since no index `j != i` exists. It does not matter — a one-element array is vacuously "all odd" or "all even" via the keep-it option. This is the case most likely to trip up an implementation that assumes a partner index is always available.
- **All elements even** (`[4, 6]`, example 2): no odd value exists to flip anything with, so the all-odd target is unreachable — but the array is already all even, so keeping everything works.
- **All elements odd**: symmetric; keep everything.
- **Exactly one odd, many evens** (`[1, 2, 4, 6]`): the case that breaks the all-*even* target, because the single odd has no other odd to pair with. The all-odd target handles it, and the single odd is reused as the subtrahend for every even. Good sanity check for anyone who derived a "need at least two odds" rule.
- **Exactly one even, many odds** (`[2, 3, 5, 7]`): the mirror case; the single even borrows any odd.
- **Reuse of `j`**: the construction leans on subtracting the same `nums1[k]` from many indices. If you mistakenly assume each `j` may be consumed only once, you will derive a much stricter condition and start returning `false` on inputs like `[1, 2, 4]`. Re-read the statement: the only restriction on `j` is `j != i`.
- **Distinct values and the `1 <= nums1[i] <= 100` bound**: neither constrains the answer. They exist to keep the input tidy, not to gate the logic — do not build them into your reasoning.
69 changes: 69 additions & 0 deletions problems/3875-construct-uniform-parity-array-i/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
number: "3875"
frontend_id: "3875"
title: "Construct Uniform Parity Array I"
slug: "construct-uniform-parity-array-i"
difficulty: "Easy"
topics:
- "Array"
- "Math"
acceptance_rate: 8061.7
is_premium: false
created_at: "2026-09-02T04:52:05.363693+00:00"
fetched_at: "2026-09-02T04:52:05.363693+00:00"
link: "https://leetcode.com/problems/construct-uniform-parity-array-i/"
date: "2026-09-02"
---

# 3875. Construct Uniform Parity Array I

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`



Return `true` if it is possible to construct such an array, otherwise, return `false`.



**Example 1:**

**Input:** nums1 = [2,3]

**Output:** true

**Explanation:**

* Choose `nums2[0] = nums1[0] - nums1[1] = 2 - 3 = -1`.
* Choose `nums2[1] = nums1[1] = 3`.
* `nums2 = [-1, 3]`, and both elements are odd. Thus, the answer is `true`​​​​​​​.



**Example 2:**

**Input:** nums1 = [4,6]

**Output:** true

**Explanation:** ​​​​​​​

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





**Constraints:**

* `1 <= n == nums1.length <= 100`
* `1 <= nums1[i] <= 100`
* `nums1` consists of distinct integers.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

// 3875. Construct Uniform Parity Array I
// https://leetcode.com/problems/construct-uniform-parity-array-i/
//
// Approach: pure parity reasoning. Only the parities of nums1 matter, and the
// move nums2[i] = nums1[i] - nums1[j] flips index i's parity exactly when
// nums1[j] is odd. Two cases cover every input:
//
// 1. nums1 is already uniform (all even or all odd): keep every element via
// nums2[i] = nums1[i].
// 2. nums1 is mixed: pick any index k holding an odd value. Keep every odd
// element, and for every even element take nums2[i] = nums1[i] - nums1[k],
// which is even - odd = odd. Since nums1[i] is even and nums1[k] is odd,
// i != k holds automatically, and nothing forbids reusing the same k for
// every even index. The result is all odd.
//
// Both cases succeed, so the answer is unconditionally true. Rather than
// returning a bare constant, this builds the witness nums2 and checks it, so
// the reasoning above is exercised instead of merely asserted.
//
// Time O(n), space O(n). Deciding the answer alone needs only O(1) of each.
func canConstructUniformParityArray(nums1 []int) bool {
return isUniformParity(buildUniformParityArray(nums1))
}

// buildUniformParityArray returns a legal nums2 whose elements share a single
// parity, following the construction described above.
func buildUniformParityArray(nums1 []int) []int {
odd := -1
for i, v := range nums1 {
if v%2 != 0 {
odd = i
break
}
}

nums2 := make([]int, len(nums1))
for i, v := range nums1 {
// Keep v when it is already odd, or when there is no odd element to
// borrow from (in which case every element is even and keeping them
// all yields a uniformly even array).
if odd == -1 || v%2 != 0 {
nums2[i] = v
continue
}
// v is even and nums1[odd] is odd, so i != odd is guaranteed.
nums2[i] = v - nums1[odd]
}
return nums2
}

// isUniformParity reports whether every element of nums shares one parity.
// An array of fewer than two elements is trivially uniform.
func isUniformParity(nums []int) bool {
for _, v := range nums {
if (v-nums[0])%2 != 0 {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import "testing"

func TestSolution(t *testing.T) {
tests := []struct {
name string
nums1 []int
expected bool
}{
{"example 1: mixed pair, all odd via 2-3", []int{2, 3}, true},
{"example 2: both even, keep both", []int{4, 6}, true},
{"edge case: single element, odd", []int{7}, true},
{"edge case: single element, even", []int{8}, true},
{"edge case: exactly one odd among many evens", []int{1, 2, 4, 6}, true},
{"edge case: exactly one even among many odds", []int{2, 3, 5, 7}, true},
{"all odd", []int{1, 3, 5, 9}, true},
{"all even", []int{2, 10, 42, 100}, true},
{"mixed, odd not first", []int{4, 7, 10, 12}, true},
{"constraint bounds: min and max values", []int{1, 100}, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := canConstructUniformParityArray(tt.nums1)
if result != tt.expected {
t.Errorf("canConstructUniformParityArray(%v) = %v, want %v", tt.nums1, result, tt.expected)
}
})
}
}

// TestBuildUniformParityArrayIsLegal checks that the witness nums2 the solution
// constructs actually obeys the rules: every entry equals nums1[i] or
// nums1[i] - nums1[j] for some j != i, and all entries share one parity.
func TestBuildUniformParityArrayIsLegal(t *testing.T) {
tests := []struct {
name string
nums1 []int
}{
{"example 1", []int{2, 3}},
{"example 2", []int{4, 6}},
{"single element", []int{7}},
{"one odd among evens", []int{1, 2, 4, 6}},
{"one even among odds", []int{2, 3, 5, 7}},
{"mixed, odd not first", []int{4, 7, 10, 12}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nums2 := buildUniformParityArray(tt.nums1)

if len(nums2) != len(tt.nums1) {
t.Fatalf("len(nums2) = %d, want %d", len(nums2), len(tt.nums1))
}
if !isUniformParity(nums2) {
t.Errorf("nums2 = %v is not uniform in parity", nums2)
}

for i, got := range nums2 {
if got == tt.nums1[i] {
continue
}
legal := false
for j := range tt.nums1 {
if j != i && got == tt.nums1[i]-tt.nums1[j] {
legal = true
break
}
}
if !legal {
t.Errorf("nums2[%d] = %d is not nums1[%d] nor nums1[%d]-nums1[j] for any j != %d",
i, got, i, i, i)
}
}
})
}
}

func TestIsUniformParity(t *testing.T) {
tests := []struct {
name string
nums []int
expected bool
}{
{"empty is trivially uniform", []int{}, true},
{"single element", []int{5}, true},
{"all odd", []int{1, 3, 5}, true},
{"all even", []int{2, 4, 6}, true},
{"negatives stay odd", []int{-1, 3, -5}, true},
{"mixed is not uniform", []int{1, 2}, false},
{"mixed with negatives", []int{-3, 4}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isUniformParity(tt.nums); got != tt.expected {
t.Errorf("isUniformParity(%v) = %v, want %v", tt.nums, got, tt.expected)
}
})
}
}