Skip to content

Commit e424117

Browse files
authored
Added tasks 3876-3892
1 parent 1b9c8fc commit e424117

45 files changed

Lines changed: 1817 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3801_3900.s3876_construct_uniform_parity_array_ii;
2+
3+
// #Medium #Array #Math #Senior #Weekly_Contest_494
4+
// #2026_08_13_Time_2_ms_(100.00%)_Space_121.98_MB_(11.30%)
5+
6+
public class Solution {
7+
public boolean uniformArray(int[] nums1) {
8+
int min = Integer.MAX_VALUE;
9+
for (int x : nums1) {
10+
min = Math.min(min, x);
11+
}
12+
if (min % 2 == 1) {
13+
return true;
14+
}
15+
for (int x : nums1) {
16+
if (x % 2 == 1) {
17+
return false;
18+
}
19+
}
20+
return true;
21+
}
22+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
3876\. Construct Uniform Parity Array II
2+
3+
Medium
4+
5+
You are given an array `nums1` of `n` **distinct** integers.
6+
7+
You want to construct another array `nums2` of length `n` such that the elements in `nums2` are either **all odd or all even**.
8+
9+
For each index `i`, you must choose **exactly one** of the following (in any order):
10+
11+
* `nums2[i] = nums1[i]`
12+
* `nums2[i] = nums1[i] - nums1[j]`, for an index `j != i`, such that `nums1[i] - nums1[j] >= 1`
13+
14+
Return `true` if it is possible to construct such an array, otherwise return `false`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums1 = [1,4,7]
19+
20+
**Output:** true
21+
22+
**Explanation:**
23+
24+
* Set `nums2[0] = nums1[0] = 1`.
25+
* Set `nums2[1] = nums1[1] - nums1[0] = 4 - 1 = 3`.
26+
* Set `nums2[2] = nums1[2] = 7`.
27+
* `nums2 = [1, 3, 7]`, and all elements are odd. Thus, the answer is `true`.
28+
29+
**Example 2:**
30+
31+
**Input:** nums1 = [2,3]
32+
33+
**Output:** false
34+
35+
**Explanation:**
36+
37+
It is not possible to construct `nums2` such that all elements have the same parity. Thus, the answer is `false`.
38+
39+
**Example 3:**
40+
41+
**Input:** nums1 = [4,6]
42+
43+
**Output:** true
44+
45+
**Explanation:**
46+
47+
* Set `nums2[0] = nums1[0] = 4`.
48+
* Set `nums2[1] = nums1[1] = 6`.
49+
* `nums2 = [4, 6]`, and all elements are even. Thus, the answer is `true`.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= n == nums1.length <= 10<sup>5</sup></code>
54+
* <code>1 <= nums1[i] <= 10<sup>9</sup></code>
55+
* `nums1` consists of distinct integers.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3801_3900.s3877_minimum_removals_to_achieve_target_xor;
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Staff #Weekly_Contest_494
4+
// #2026_08_13_Time_10_ms_(100.00%)_Space_48.52_MB_(75.00%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int minRemovals(int[] nums, int target) {
10+
int max = 0;
11+
for (int n : nums) {
12+
max = Math.max(max, n);
13+
}
14+
int u = 1 << (32 - Integer.numberOfLeadingZeros(max));
15+
if (target >= u) {
16+
return -1;
17+
}
18+
int n = nums.length;
19+
int[][] f = new int[n + 1][u];
20+
Arrays.fill(f[0], Integer.MAX_VALUE / 2);
21+
f[0][0] = 0;
22+
for (int i = 0; i < n; i++) {
23+
for (int x = 0; x < u; x++) {
24+
f[i + 1][x] = Math.min(f[i][x] + 1, f[i][x ^ nums[i]]);
25+
}
26+
}
27+
return f[n][target] == Integer.MAX_VALUE / 2 ? -1 : f[n][target];
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3877\. Minimum Removals to Achieve Target XOR
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `target`.
6+
7+
You may remove **any** number of elements from `nums` (possibly zero).
8+
9+
Return the **minimum** number of removals required so that the **bitwise XOR** of the remaining elements equals `target`. If it is impossible to achieve `target`, return -1.
10+
11+
The bitwise XOR of an empty array is 0.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3], target = 2
16+
17+
**Output:** 1
18+
19+
**Explanation:**
20+
21+
* Removing `nums[1] = 2` leaves `[nums[0], nums[2]] = [1, 3]`.
22+
* The XOR of `[1, 3]` is 2, which equals `target`.
23+
* It is not possible to achieve XOR = 2 in less than one removal, therefore the answer is 1.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [2,4], target = 1
28+
29+
**Output:** \-1
30+
31+
**Explanation:**
32+
33+
It is impossible to remove elements to achieve `target`. Thus, the answer is -1.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [7], target = 7
38+
39+
**Output:** 0
40+
41+
**Explanation:**
42+
43+
The XOR of all elements is `nums[0] = 7`, which equals `target`. Thus, no removal is needed.
44+
45+
**Constraints:**
46+
47+
* `1 <= nums.length <= 40`
48+
* <code>0 <= nums[i] <= 10<sup>4</sup></code>
49+
* <code>0 <= target <= 10<sup>4</sup></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3801_3900.s3878_count_good_subarrays;
2+
3+
// #Hard #Array #Bit_Manipulation #Stack #Monotonic_Stack #Senior_Staff #Weekly_Contest_494
4+
// #2026_08_13_Time_6_ms_(100.00%)_Space_105.66_MB_(98.88%)
5+
6+
public class Solution {
7+
public long countGoodSubarrays(int[] nums) {
8+
int n = nums.length;
9+
long ans = 0;
10+
int[] prev = new int[n];
11+
for (int i = 0; i < n; i++) {
12+
int left = i - 1;
13+
while (left >= 0 && (nums[left] | nums[i]) == nums[i]) {
14+
left = prev[left];
15+
}
16+
prev[i] = left;
17+
int right = i + 1;
18+
while (right < n && nums[right] != nums[i] && (nums[right] | nums[i]) == nums[i]) {
19+
right++;
20+
}
21+
ans += (long) (i - left) * (right - i);
22+
}
23+
return ans;
24+
}
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3878\. Count Good Subarrays
2+
3+
Hard
4+
5+
You are given an integer array `nums`.
6+
7+
A **non-empty subarrays** is called **good** if the **bitwise OR** of all its elements is equal to **at least one** element present in that subarray.
8+
9+
Return the number of good subarrays in `nums`.
10+
11+
Here, the bitwise OR of two integers `a` and `b` is denoted by `a | b`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [4,2,3]
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
The subarrays of `nums` are:
22+
23+
| Subarray | Bitwise OR | Present in Subarray |
24+
| ----------- | ----------------- | ------------------- |
25+
| `[4]` | `4 = 4` | Yes |
26+
| `[2]` | `2 = 2` | Yes |
27+
| `[3]` | `3 = 3` | Yes |
28+
| `[4, 2]` | `4 \| 2 = 6` | No |
29+
| `[2, 3]` | `2 \| 3 = 3` | Yes |
30+
| `[4, 2, 3]` | `4 \| 2 \| 3 = 7` | No |
31+
32+
Thus, the good subarrays of `nums` are `[4]`, `[2]`, `[3]` and `[2, 3]`. Thus, the answer is 4.
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [1,3,1]
37+
38+
**Output:** 6
39+
40+
**Explanation:**
41+
42+
Any subarray of `nums` containing 3 has bitwise OR equal to 3, and subarrays containing only 1 have bitwise OR equal to 1.
43+
44+
In both cases, the result is present in the subarray, so all subarrays are good, and the answer is 6.
45+
46+
**Constraints:**
47+
48+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
49+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3801_3900.s3880_minimum_absolute_difference_between_two_values;
2+
3+
// #Easy #Array #Enumeration #Mid_Level #Biweekly_Contest_179
4+
// #2026_08_13_Time_1_ms_(99.36%)_Space_46.20_MB_(85.90%)
5+
6+
public class Solution {
7+
public int minAbsoluteDifference(int[] nums) {
8+
int min = Integer.MAX_VALUE;
9+
int n = nums.length;
10+
int prev = -1;
11+
int last = -1;
12+
for (int i = 0; i < n; i++) {
13+
if (prev == -1) {
14+
if (nums[i] == 1) {
15+
prev = 1;
16+
last = i;
17+
} else if (nums[i] == 2) {
18+
prev = 2;
19+
last = i;
20+
}
21+
} else {
22+
if (nums[i] == 1) {
23+
if (prev == 2) {
24+
min = Math.min(min, i - last);
25+
prev = 1;
26+
}
27+
last = i;
28+
} else if (nums[i] == 2) {
29+
if (prev == 1) {
30+
min = Math.min(min, i - last);
31+
prev = 2;
32+
}
33+
last = i;
34+
}
35+
}
36+
}
37+
return min != Integer.MAX_VALUE ? min : -1;
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3880\. Minimum Absolute Difference Between Two Values
2+
3+
Easy
4+
5+
You are given an integer array `nums` consisting only of 0, 1, and 2.
6+
7+
A pair of indices `(i, j)` is called **valid** if `nums[i] == 1` and `nums[j] == 2`.
8+
9+
Return the **minimum** absolute difference between `i` and `j` among all valid pairs. If no valid pair exists, return -1.
10+
11+
The absolute difference between indices `i` and `j` is defined as `abs(i - j)`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,0,0,2,0,1]
16+
17+
**Output:** 2
18+
19+
**Explanation:**
20+
21+
The valid pairs are:
22+
23+
* (0, 3) which has absolute difference of `abs(0 - 3) = 3`.
24+
* (5, 3) which has absolute difference of `abs(5 - 3) = 2`.
25+
26+
Thus, the answer is 2.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [1,0,1,0]
31+
32+
**Output:** \-1
33+
34+
**Explanation:**
35+
36+
There are no valid pairs in the array, thus the answer is -1.
37+
38+
**Constraints:**
39+
40+
* `1 <= nums.length <= 100`
41+
* `0 <= nums[i] <= 2`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3801_3900.s3881_direction_assignments_with_exactly_k_visible_people;
2+
3+
// #Medium #Math #Combinatorics #Staff #Biweekly_Contest_179
4+
// #2026_08_13_Time_4_ms_(100.00%)_Space_42.91_MB_(67.27%)
5+
6+
@SuppressWarnings("java:S1172")
7+
public class Solution {
8+
private static final long MOD = 1_000_000_007L;
9+
10+
public int countVisiblePeople(int n, int pos, int k) {
11+
int total = n - 1;
12+
long combinations = nCr(total, k);
13+
return (int) (2L * combinations % MOD);
14+
}
15+
16+
private long nCr(int n, int r) {
17+
r = Math.min(r, n - r);
18+
long numerator = 1;
19+
long denominator = 1;
20+
for (int i = 1; i <= r; i++) {
21+
numerator = numerator * (n - r + i) % MOD;
22+
denominator = denominator * i % MOD;
23+
}
24+
return numerator * modPow(denominator, MOD - 2) % MOD;
25+
}
26+
27+
private long modPow(long base, long exponent) {
28+
long result = 1;
29+
while (exponent > 0) {
30+
if ((exponent & 1) == 1) {
31+
result = result * base % MOD;
32+
}
33+
34+
base = base * base % MOD;
35+
exponent >>= 1;
36+
}
37+
return result;
38+
}
39+
}

0 commit comments

Comments
 (0)