diff --git a/Easy/3875.Construct-Uniform-Parity-Array-I/description.md b/Easy/3875.Construct-Uniform-Parity-Array-I/description.md new file mode 100644 index 0000000..7f31ec4 --- /dev/null +++ b/Easy/3875.Construct-Uniform-Parity-Array-I/description.md @@ -0,0 +1,43 @@ +# 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 + +```text +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 + +```text +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. diff --git a/Easy/3875.Construct-Uniform-Parity-Array-I/solution.md b/Easy/3875.Construct-Uniform-Parity-Array-I/solution.md new file mode 100644 index 0000000..dfcdeb7 --- /dev/null +++ b/Easy/3875.Construct-Uniform-Parity-Array-I/solution.md @@ -0,0 +1,130 @@ +# Intuition + +Only parity matters, and the two moves have fixed parity effects: keeping +`nums1[i]` preserves its parity, while `nums1[i] - nums1[j]` flips it when +`nums1[j]` is odd and preserves it when `nums1[j]` is even. So the whole problem +is "can every element be pushed to one common parity?". + +Aiming for **all odd** answers that immediately. Odd elements are already odd and +just stay put; an even element becomes odd by subtracting any odd element. A +single odd element in the array serves as the donor for *every* even element, +because the constraint on `j` is only `j != i` — nothing stops one index from +being reused. And if the array has no odd element at all, it is already all even. + +Either way the construction succeeds, so the answer is always `true` and the input +never has to be inspected. + +# Approach: Parity Argument (Always Possible) + +Let `k` be the number of odd values in `nums1`. + +- **`k = 0`.** Every element is even. Choose `nums2[i] = nums1[i]` for all `i`; + `nums2` is all even. +- **`k >= 1`.** Fix any index `p` with `nums1[p]` odd. For each `i`: + - if `nums1[i]` is odd, choose `nums2[i] = nums1[i]`; + - if `nums1[i]` is even, then `i != p` (the two have different parity, so they + cannot be the same index), and `nums2[i] = nums1[i] - nums1[p]` is + `even - odd = odd`. + + Every entry is odd, so `nums2` is all odd. + +Both cases produce a valid `nums2`, so `return true` unconditionally. + +## Why aim for odd and not even + +The symmetric attempt — force everything even — genuinely fails, which is worth +seeing because it is the only place the problem has any tension. An odd element +can only be made even by subtracting *another odd* element, so an array with +exactly one odd value has no way to neutralise it. Example 1, `nums1 = [2,3]`, is +exactly that shape: `3` cannot become even, but `2 - 3 = -1` makes the array all +odd instead. + +The odd target has no such dependency: an even element needs an odd donor, and by +definition of the case `k >= 1` at least one exists, and it is never the element +being converted. Conversions never consume the donor, so one odd value is enough +no matter how many evens there are. + +## What the constraints do not matter for + +- **Distinctness.** The proof only needs "an odd index differs from an even + index", which follows from parity alone. The answer stays `true` with + duplicates. +- **Positivity.** Nothing depends on `1 <= nums1[i] <= 100`; the differences may + be negative — `-1` in Example 1 is — and parity is unaffected by sign. +- **`n = 1`.** The branch taken for a single element only ever uses + `nums2[0] = nums1[0]`, which is the sole legal move when no `j != i` exists. A + one-element array is trivially uniform. + +# Worked examples + +## `nums1 = [2,3]` → `true` + +`k = 1`, so take `p = 1` (the odd `3`). + +| `i` | `nums1[i]` | parity | choice | `nums2[i]` | +| --- | ---------- | ------ | ------------------- | ---------- | +| `0` | `2` | even | subtract `nums1[1]` | `-1` | +| `1` | `3` | odd | keep | `3` | + +`nums2 = [-1, 3]` — all odd. This matches the official explanation. + +## `nums1 = [4,6]` → `true` + +`k = 0`, so keep both: `nums2 = [4, 6]`, all even. No subtraction is needed. + +## `nums1 = [5,8,12,20]` → `true` + +`k = 1` again, with the lone odd `5` at index `0` acting as donor for all three +evens: `nums2 = [5, 3, 7, 15]`, all odd. One donor, reused three times. + +# Complexity + +- Time complexity: $$O(1)$$ — the answer is a constant, so no element of `nums1` + is read. Even a version that counted odds first would be $$O(n)$$, where `n` is + the length of `nums1`. +- Space complexity: $$O(1)$$. + +# Code + +## Go + +```go +func uniformArray(nums1 []int) bool { + return true +} +``` + +## Rust + +```rust +impl Solution { + pub fn uniform_array(nums1: Vec) -> bool { + true + } +} +``` + +## Python + +```python +class Solution: + def uniformArray(self, nums1: list[int]) -> bool: + return True +``` + +# Test cases + +| `nums1` | answer | why | +| ------------------ | ------ | ---------------------------------------------- | +| `[2,3]` | `true` | Example 1 — all odd via `2 - 3 = -1` | +| `[4,6]` | `true` | Example 2 — already all even, `k = 0` | +| `[7]` | `true` | single element, no `j` available | +| `[2,4,6,8]` | `true` | `k = 0`, keep everything | +| `[1,3,5,7]` | `true` | already all odd, keep everything | +| `[5,8,12,20]` | `true` | one odd donor reused by every even | +| `[1..100]` | `true` | maximal input, both parities present | + +An exhaustive check confirmed the constant answer: for every distinct-valued array +drawn from `1..10` with length `1` to `6` (847 arrays), a brute force over all +$$\prod_i |\text{choices}_i|$$ assignments found a uniform-parity `nums2` in every +case. diff --git a/README.md b/README.md index 2ed7ad4..e7704bc 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ Easy/350.Intersection-of-Two-Arrays-II/ ## Solutions index -Total: **208** problems with at least one solution file. +Total: **209** problems with at least one solution file. Solution links use variant names when multiple approaches or languages exist (`main` = `solution.md`, others = `solution-.md`). -### Easy (53) +### Easy (54) | Problem | LeetCode | Solution | | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | @@ -80,6 +80,7 @@ Solution links use variant names when multiple approaches or languages exist (`m | 3718. Smallest Missing Multiple of K | [Link](https://leetcode.com/problems/smallest-missing-multiple-of-k/) | [main](Easy/3718.Smallest-Missing-Multiple-of-K/solution.md) | | 3731. Find Missing Elements | [Link](https://leetcode.com/problems/find-missing-elements/) | [main](Easy/3731.Find-Missing-Elements/solution.md) | | 3754. Concatenate Non-Zero Digits and Multiply by Sum I | [Link](https://leetcode.com/problems/concatenate-non-zero-digits-and-multiply-by-sum-i/) | [main](Easy/3754.Concatenate-Non-Zero-Digits-and-Multiply-by-Sum-I/solution.md) | +| 3875. Construct Uniform Parity Array I | [Link](https://leetcode.com/problems/construct-uniform-parity-array-i/) | [main](Easy/3875.Construct-Uniform-Parity-Array-I/solution.md) | ### Medium (121) diff --git a/SUMMARY.md b/SUMMARY.md index 6ea74fe..f350c78 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -62,6 +62,7 @@ * [3718. Smallest Missing Multiple of K](Easy/3718.Smallest-Missing-Multiple-of-K/solution.md) * [3731. Find Missing Elements](Easy/3731.Find-Missing-Elements/solution.md) * [3754. Concatenate Non Zero Digits and Multiply by Sum I](Easy/3754.Concatenate-Non-Zero-Digits-and-Multiply-by-Sum-I/solution.md) +* [3875. Construct Uniform Parity Array I](Easy/3875.Construct-Uniform-Parity-Array-I/solution.md) ## Medium diff --git a/_sidebar.md b/_sidebar.md index 0b7ca4b..c652dab 100644 --- a/_sidebar.md +++ b/_sidebar.md @@ -59,6 +59,7 @@ - [3718. Smallest Missing Multiple of K](Easy/3718.Smallest-Missing-Multiple-of-K/solution.md) - [3731. Find Missing Elements](Easy/3731.Find-Missing-Elements/solution.md) - [3754. Concatenate Non Zero Digits and Multiply by Sum I](Easy/3754.Concatenate-Non-Zero-Digits-and-Multiply-by-Sum-I/solution.md) + - [3875. Construct Uniform Parity Array I](Easy/3875.Construct-Uniform-Parity-Array-I/solution.md) - Medium - [33. Search in rotated sorted array](Medium/33.Search-in-rotated-sorted-array/solution.md) - [40. Combination Sum II](Medium/40.Combination-Sum-II/solution.md)