Skip to content
Merged
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
43 changes: 43 additions & 0 deletions Easy/3875.Construct-Uniform-Parity-Array-I/description.md
Original file line number Diff line number Diff line change
@@ -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.
130 changes: 130 additions & 0 deletions Easy/3875.Construct-Uniform-Parity-Array-I/solution.md
Original file line number Diff line number Diff line change
@@ -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<i32>) -> 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.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<variant>.md`).

### Easy (53)
### Easy (54)

| Problem | LeetCode | Solution |
| ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions _sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading