From c4e02d0f8c0bf910ad662f6b36469b18740b25d9 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 3 Sep 2026 04:52:07 +0000 Subject: [PATCH] feat: add solution for 3876. Construct Uniform Parity Array II --- .../analysis.md | 63 +++++++++++++++ .../problem.md | 80 +++++++++++++++++++ .../solution_daily_20260903.go | 45 +++++++++++ .../solution_daily_20260903_test.go | 33 ++++++++ 4 files changed, 221 insertions(+) create mode 100644 problems/3876-construct-uniform-parity-array-ii/analysis.md create mode 100644 problems/3876-construct-uniform-parity-array-ii/problem.md create mode 100644 problems/3876-construct-uniform-parity-array-ii/solution_daily_20260903.go create mode 100644 problems/3876-construct-uniform-parity-array-ii/solution_daily_20260903_test.go diff --git a/problems/3876-construct-uniform-parity-array-ii/analysis.md b/problems/3876-construct-uniform-parity-array-ii/analysis.md new file mode 100644 index 0000000..0c5cb7c --- /dev/null +++ b/problems/3876-construct-uniform-parity-array-ii/analysis.md @@ -0,0 +1,63 @@ +# 3876. Construct Uniform Parity Array II + +[LeetCode Link](https://leetcode.com/problems/construct-uniform-parity-array-ii/) + +Difficulty: Medium +Topics: Array, Math +Acceptance Rate: 57.8% + +## Hints + +### Hint 1 + +The problem talks about "all odd or all even", so nothing about the actual magnitude of the numbers matters except through parity. Try splitting the decision into two completely independent questions: *can I make everything even?* and *can I make everything odd?* The answer is `true` if either sub-question is `true`. Also remember the only arithmetic fact you need: `even - even = even`, `odd - odd = even`, `even - odd = odd`, `odd - even = odd`. + +### Hint 2 + +Fix a target parity and look at one index at a time. An element that already has the target parity can just be kept (`nums2[i] = nums1[i]`), so it is never a problem. An element with the *wrong* parity is forced to use the subtraction option, and the parity rules above pin down exactly the parity of the partner `nums1[j]` it needs. On top of parity, the constraint `nums1[i] - nums1[j] >= 1` means the partner has to be **strictly smaller** than it. So each "wrong parity" element needs a smaller element of one specific parity — and nothing stops many indices from reusing the same partner `j`. + +### Hint 3 + +Apply Hint 2 to each target separately. + +- **All even:** every odd element needs a strictly smaller *odd* partner. The smallest odd element in the array has no smaller odd element, so it can never be fixed. Therefore all-even is possible **only when there are no odd elements at all**. +- **All odd:** every even element needs a strictly smaller *odd* partner. A single odd element can serve every one of them, and the best candidate is the smallest odd value. So all-odd is possible exactly when at least one odd exists and `min(odd) < min(even)`. + +That reduces the whole problem to two minimums in one pass — no sorting, no searching over pairs. + +## Approach + +Scan the array once and track two values: + +- `minOdd` — the smallest odd element (absent if the array has no odd numbers) +- `minEven` — the smallest even element (absent if the array has no even numbers) + +Then decide: + +1. **No odd elements.** Every element is already even, so keep everything as-is: `nums2 = nums1` is all even. Return `true`. +2. **No even elements.** Every element is already odd, so keep everything as-is. Return `true`. +3. **Both parities present.** All-even is impossible (case 1's argument: the smallest odd has no smaller odd to subtract, and subtracting an even keeps it odd). So the only hope is all-odd. Each even element `x` must become `x - y` for some odd `y < x`; using `y = minOdd` is optimal, because if `minOdd` fails for some `x` then no odd value works for it. So the answer is `minOdd < minEven`: if the smallest odd beats the smallest even, it beats every even value, and every even element can be repaired. Otherwise the smallest even element is stuck. + +Note that reuse is free — the problem lets any number of indices `i` pick the same partner `j` — which is why one well-chosen `minOdd` is enough for all evens at once. Also, the odd elements themselves need no work in the all-odd case; they simply keep their original value. + +Worked example, `nums1 = [1, 4, 7]`: `minOdd = 1`, `minEven = 4`. Both parities appear, and `1 < 4`, so the answer is `true`. Concretely, `1` and `7` stay put, and `4` becomes `4 - 1 = 3`, giving `[1, 3, 7]`. + +Counter-example, `nums1 = [2, 3]`: `minOdd = 3`, `minEven = 2`, and `3 < 2` is false. The element `2` has no smaller odd partner (`3` is larger, so `2 - 3 = -1` violates the `>= 1` rule), and `3` has no smaller odd partner either, so neither target parity is reachable — `false`. + +The distinctness guarantee is a convenience rather than a load-bearing fact here; the comparison `minOdd < minEven` is between values of different parity, so they can never be equal anyway. + +This is a Medium that is genuinely easy *once* you make the "solve each target parity separately" split. The trap is trying to reason about both targets at once, or trying to match each element to a partner greedily — that leads to needless sorting or pairing logic. + +## Complexity Analysis + +Time Complexity: O(n) — a single pass over `nums1`. +Space Complexity: O(1) — only the two running minimums. + +## Edge Cases + +- **`n == 1`.** The subtraction option requires an index `j != i`, so it is unavailable; but keeping the single element always yields a uniform array. The code returns `true` because one of the two "only one parity present" branches fires. +- **All elements even.** Return `true` immediately by keeping everything. Do not fall into the `minOdd < minEven` comparison with a missing `minOdd`. +- **All elements odd.** Same idea in the other direction — `true`, with no `minEven` to compare against. Both of these are why the absence of a parity must be tracked with a sentinel rather than assuming both minimums exist. +- **Smallest even below the smallest odd** (e.g. `[2, 3]`, or `[2, 5, 7, 9]`). The smallest even can never be repaired, so the answer is `false` even though every other even element might have been fixable. +- **Smallest odd immediately below the smallest even** (e.g. `[1, 2]`). The strict inequality is what matters; since parities differ the values can never tie, so `<` and `<=` behave identically, but `<` states the intent correctly. +- **Large values (up to 1e9) and large `n` (up to 1e5).** Values fit comfortably in Go's `int`, and the single pass avoids any O(n log n) or O(n^2) work. diff --git a/problems/3876-construct-uniform-parity-array-ii/problem.md b/problems/3876-construct-uniform-parity-array-ii/problem.md new file mode 100644 index 0000000..d28026a --- /dev/null +++ b/problems/3876-construct-uniform-parity-array-ii/problem.md @@ -0,0 +1,80 @@ +--- +number: "3876" +frontend_id: "3876" +title: "Construct Uniform Parity Array II" +slug: "construct-uniform-parity-array-ii" +difficulty: "Medium" +topics: + - "Array" + - "Math" +acceptance_rate: 5775.9 +is_premium: false +created_at: "2026-09-03T04:50:03.343385+00:00" +fetched_at: "2026-09-03T04:50:03.343385+00:00" +link: "https://leetcode.com/problems/construct-uniform-parity-array-ii/" +date: "2026-09-03" +--- + +# 3876. Construct Uniform Parity Array II + +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. diff --git a/problems/3876-construct-uniform-parity-array-ii/solution_daily_20260903.go b/problems/3876-construct-uniform-parity-array-ii/solution_daily_20260903.go new file mode 100644 index 0000000..57692dc --- /dev/null +++ b/problems/3876-construct-uniform-parity-array-ii/solution_daily_20260903.go @@ -0,0 +1,45 @@ +package main + +// 3876. Construct Uniform Parity Array II +// +// Solve the two target parities independently and take the OR. +// +// An element that already has the target parity can simply be kept, so only +// wrong-parity elements matter, and each of them must use the subtraction +// option with a partner that is both of the required parity and strictly +// smaller (the result has to be >= 1). Partners may be reused freely. +// +// - All even: every odd element needs a strictly smaller odd partner, which +// the smallest odd element can never have. So all-even works only when the +// array contains no odd elements at all. +// - All odd: every even element needs a strictly smaller odd partner, and the +// smallest odd value is the best possible choice for all of them at once. +// So all-odd works when there is an odd element and min(odd) < min(even). +// +// One pass tracking the two minimums is enough: O(n) time, O(1) space. +func canConstructUniformParity(nums1 []int) bool { + const none = -1 + minOdd, minEven := none, none + + for _, v := range nums1 { + if v%2 != 0 { + if minOdd == none || v < minOdd { + minOdd = v + } + } else { + if minEven == none || v < minEven { + minEven = v + } + } + } + + // Only one parity present (including the empty and single-element cases): + // keep every element as it is. + if minOdd == none || minEven == none { + return true + } + + // Both parities present: all-even is out, so the smallest odd must be able + // to repair every even element, i.e. beat the smallest even one. + return minOdd < minEven +} diff --git a/problems/3876-construct-uniform-parity-array-ii/solution_daily_20260903_test.go b/problems/3876-construct-uniform-parity-array-ii/solution_daily_20260903_test.go new file mode 100644 index 0000000..0a833c0 --- /dev/null +++ b/problems/3876-construct-uniform-parity-array-ii/solution_daily_20260903_test.go @@ -0,0 +1,33 @@ +package main + +import "testing" + +func TestCanConstructUniformParity(t *testing.T) { + tests := []struct { + name string + nums1 []int + expected bool + }{ + {"example 1: [1,4,7] smallest odd 1 repairs the even 4", []int{1, 4, 7}, true}, + {"example 2: [2,3] smallest even has no smaller odd partner", []int{2, 3}, false}, + {"example 3: [4,6] already all even", []int{4, 6}, true}, + {"edge case: single odd element, subtraction unavailable", []int{5}, true}, + {"edge case: single even element, subtraction unavailable", []int{4}, true}, + {"edge case: all odd stays as-is", []int{9, 3, 7, 1}, true}, + {"edge case: all even stays as-is", []int{1000000000, 2, 48}, true}, + {"edge case: min odd is exactly one below min even", []int{1, 2}, true}, + {"edge case: min even below every odd", []int{2, 5, 7, 9}, false}, + {"edge case: many evens repaired by a single small odd", []int{3, 4, 6, 8, 100}, true}, + {"edge case: one unreachable even blocks an otherwise fixable array", []int{2, 3, 10, 20}, false}, + {"edge case: large values with odd minimum", []int{999999999, 1000000000}, true}, + {"edge case: large values with even minimum", []int{1000000000, 1000000001}, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := canConstructUniformParity(tt.nums1); got != tt.expected { + t.Errorf("canConstructUniformParity(%v) = %v, want %v", tt.nums1, got, tt.expected) + } + }) + } +}