Skip to content

Commit 06211c3

Browse files
authored
Added tasks 3737-3747
1 parent 6c33c0a commit 06211c3

30 files changed

Lines changed: 1015 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3701_3800.s3737_count_subarrays_with_majority_element_i;
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #Counting #Divide_and_Conquer #Segment_Tree #Merge_Sort
4+
// #Senior #Biweekly_Contest_169 #2026_04_26_Time_1_ms_(100.00%)_Space_46.96_MB_(57.62%)
5+
6+
public class Solution {
7+
public int countMajoritySubarrays(int[] a, int target) {
8+
int n = a.length;
9+
int pre = n + 1;
10+
int res = 0;
11+
int[] count = new int[2 * n + 2];
12+
int[] acc = new int[2 * n + 2];
13+
count[pre] = acc[pre] = 1;
14+
for (int i : a) {
15+
pre += (i == target ? 1 : -1);
16+
count[pre]++;
17+
acc[pre] = acc[pre - 1] + count[pre];
18+
res += acc[pre - 1];
19+
}
20+
return res;
21+
}
22+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3737\. Count Subarrays With Majority Element I
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `target`.
6+
7+
Return the number of **non-empty subarrays** of `nums` in which `target` is the **majority element**.
8+
9+
The **majority element** of a subarray is the element that appears **strictly** **more than half** of the times in that subarray.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,2,3], target = 2
14+
15+
**Output:** 5
16+
17+
**Explanation:**
18+
19+
Valid subarrays with `target = 2` as the majority element:
20+
21+
* `nums[1..1] = [2]`
22+
* `nums[2..2] = [2]`
23+
* `nums[1..2] = [2,2]`
24+
* `nums[0..2] = [1,2,2]`
25+
* `nums[1..3] = [2,2,3]`
26+
27+
So there are 5 such subarrays.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,1,1,1], target = 1
32+
33+
**Output:** 10
34+
35+
**Explanation:**
36+
37+
All 10 subarrays have 1 as the majority element.
38+
39+
**Example 3:**
40+
41+
**Input:** nums = [1,2,3], target = 4
42+
43+
**Output:** 0
44+
45+
**Explanation:**
46+
47+
`target = 4` does not appear in `nums` at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0.
48+
49+
**Constraints:**
50+
51+
* `1 <= nums.length <= 1000`
52+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
53+
* <code>1 <= target <= 10<sup>9</sup></code>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3701_3800.s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element;
2+
3+
// #Medium #Array #Dynamic_Programming #Staff #Biweekly_Contest_169
4+
// #2026_04_26_Time_7_ms_(99.28%)_Space_123.88_MB_(5.07%)
5+
6+
public class Solution {
7+
public int longestSubarray(int[] nums) {
8+
int n = nums.length;
9+
if (n < 3) {
10+
return n;
11+
}
12+
int i = 0;
13+
int len1 = 0;
14+
int len2 = 0;
15+
int ans = 2;
16+
while (i < n) {
17+
int l = (i == 0) ? -1000 * 1000 * 10 : nums[i - 1];
18+
int r = (i == n - 1) ? 1000 * 1000 * 10 : nums[i + 1];
19+
if (l <= nums[i]) {
20+
len2++;
21+
ans = Math.max(len2 + 1, ans);
22+
} else if (r >= nums[i]) {
23+
int j = i;
24+
len1 = len2;
25+
while (j < n - 1 && nums[j] <= nums[j + 1]) {
26+
j++;
27+
}
28+
len2 = j - i + 1;
29+
ans = Math.max(len2 + 1, ans);
30+
if (l <= r || (len1 > 1 && nums[i - 2] <= nums[i])) {
31+
ans = Math.max(len1 + len2, ans);
32+
}
33+
i = j;
34+
} else {
35+
len2 = 0;
36+
}
37+
i++;
38+
}
39+
ans = Math.min(ans, n);
40+
return ans;
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3738\. Longest Non-Decreasing Subarray After Replacing at Most One Element
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
You are allowed to replace **at most** one element in the array with any other integer value of your choice.
8+
9+
Return the length of the **longest non-decreasing subarray** that can be obtained after performing at most one replacement.
10+
11+
An array is said to be **non-decreasing** if each element is greater than or equal to its previous one (if it exists).
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,1,2]
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
Replacing `nums[3] = 1` with 3 gives the array [1, 2, 3, 3, 2].
22+
23+
The longest non-decreasing subarray is [1, 2, 3, 3], which has a length of 4.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [2,2,2,2,2]
28+
29+
**Output:** 5
30+
31+
**Explanation:**
32+
33+
All elements in `nums` are equal, so it is already non-decreasing and the entire `nums` forms a subarray of length 5.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g3701_3800.s3739_count_subarrays_with_majority_element_ii;
2+
3+
// #Hard #Array #Hash_Table #Prefix_Sum #Divide_and_Conquer #Segment_Tree #Merge_Sort #Senior_Staff
4+
// #Biweekly_Contest_169 #2026_04_26_Time_3_ms_(100.00%)_Space_90.94_MB_(36.19%)
5+
6+
public class Solution {
7+
public long countMajoritySubarrays(int[] nums, int target) {
8+
int n = nums.length;
9+
int pre = n + 1;
10+
long[] count = new long[2 * n + 2];
11+
long[] acc = new long[2 * n + 2];
12+
long res = 0;
13+
count[pre] = acc[pre] = 1;
14+
for (int a : nums) {
15+
pre += (a == target ? 1 : -1);
16+
acc[pre] = ++count[pre] + acc[pre - 1];
17+
res += acc[pre - 1];
18+
}
19+
return res;
20+
}
21+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3739\. Count Subarrays With Majority Element II
2+
3+
Hard
4+
5+
You are given an integer array `nums` and an integer `target`.
6+
7+
Return the number of **non-empty subarrays** of `nums` in which `target` is the **majority element**.
8+
9+
The **majority element** of a subarray is the element that appears **strictly more than half** of the times in that subarray.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,2,3], target = 2
14+
15+
**Output:** 5
16+
17+
**Explanation:**
18+
19+
Valid subarrays with `target = 2` as the majority element:
20+
21+
* `nums[1..1] = [2]`
22+
* `nums[2..2] = [2]`
23+
* `nums[1..2] = [2,2]`
24+
* `nums[0..2] = [1,2,2]`
25+
* `nums[1..3] = [2,2,3]`
26+
27+
So there are 5 such subarrays.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,1,1,1], target = 1
32+
33+
**Output:** 10
34+
35+
**Explanation:**
36+
37+
All 10 subarrays have 1 as the majority element.
38+
39+
**Example 3:**
40+
41+
**Input:** nums = [1,2,3], target = 4
42+
43+
**Output:** 0
44+
45+
**Explanation:**
46+
47+
`target = 4` does not appear in `nums` at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
52+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
53+
* <code>1 <= target <= 10<sup>9</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3701_3800.s3740_minimum_distance_between_three_equal_elements_i;
2+
3+
// #Easy #Array #Hash_Table #Mid_Level #Weekly_Contest_475
4+
// #2026_04_26_Time_1_ms_(99.99%)_Space_44.21_MB_(75.36%)
5+
6+
public class Solution {
7+
public int minimumDistance(int[] nums) {
8+
int len = nums.length;
9+
int[] last2 = new int[len];
10+
int res = 200;
11+
for (int i = 0; i < len; i++) {
12+
int val = nums[i] - 1;
13+
int pos = i + 1;
14+
int pack = last2[val];
15+
int old = pack & 255;
16+
int cur = pack >> 8;
17+
last2[val] = cur | (pos << 8);
18+
if (old > 0) {
19+
res = Math.min(res, (pos - old) << 1);
20+
}
21+
}
22+
return res == 200 ? -1 : res;
23+
}
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3740\. Minimum Distance Between Three Equal Elements I
2+
3+
Easy
4+
5+
You are given an integer array `nums`.
6+
7+
A tuple `(i, j, k)` of 3 **distinct** indices is **good** if `nums[i] == nums[j] == nums[k]`.
8+
9+
The **distance** of a **good** tuple is `abs(i - j) + abs(j - k) + abs(k - i)`, where `abs(x)` denotes the **absolute value** of `x`.
10+
11+
Return an integer denoting the **minimum** possible **distance** of a **good** tuple. If no **good** tuples exist, return `-1`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,1,1,3]
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
The minimum distance is achieved by the good tuple `(0, 2, 3)`.
22+
23+
`(0, 2, 3)` is a good tuple because `nums[0] == nums[2] == nums[3] == 1`. Its distance is `abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6`.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,1,2,3,2,1,2]
28+
29+
**Output:** 8
30+
31+
**Explanation:**
32+
33+
The minimum distance is achieved by the good tuple `(2, 4, 6)`.
34+
35+
`(2, 4, 6)` is a good tuple because `nums[2] == nums[4] == nums[6] == 2`. Its distance is `abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8`.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [1]
40+
41+
**Output:** \-1
42+
43+
**Explanation:**
44+
45+
There are no good tuples. Therefore, the answer is -1.
46+
47+
**Constraints:**
48+
49+
* `1 <= n == nums.length <= 100`
50+
* `1 <= nums[i] <= n`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3701_3800.s3741_minimum_distance_between_three_equal_elements_ii;
2+
3+
// #Medium #Array #Hash_Table #Senior #Weekly_Contest_475
4+
// #2026_04_26_Time_6_ms_(99.60%)_Space_161.24_MB_(96.23%)
5+
6+
public class Solution {
7+
public int minimumDistance(int[] nums) {
8+
int n = nums.length;
9+
int ans = Integer.MAX_VALUE;
10+
int[] prev1 = new int[n + 1];
11+
int[] prev2 = new int[n + 1];
12+
for (int i = 0; i < n + 1; i++) {
13+
prev1[i] = prev2[i] = -1;
14+
}
15+
for (int i = 0; i < n; i++) {
16+
int value = nums[i];
17+
if (prev2[value] != -1) {
18+
ans = Math.min(ans, (i - prev2[value]));
19+
}
20+
prev2[value] = prev1[value];
21+
prev1[value] = i;
22+
}
23+
if (ans < 100002) {
24+
return ans * 2;
25+
}
26+
return -1;
27+
}
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3741\. Minimum Distance Between Three Equal Elements II
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
A tuple `(i, j, k)` of 3 **distinct** indices is **good** if `nums[i] == nums[j] == nums[k]`.
8+
9+
The **distance** of a **good** tuple is `abs(i - j) + abs(j - k) + abs(k - i)`, where `abs(x)` denotes the **absolute value** of `x`.
10+
11+
Return an integer denoting the **minimum** possible **distance** of a **good** tuple. If no **good** tuples exist, return `-1`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,1,1,3]
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
The minimum distance is achieved by the good tuple `(0, 2, 3)`.
22+
23+
`(0, 2, 3)` is a good tuple because `nums[0] == nums[2] == nums[3] == 1`. Its distance is `abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6`.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,1,2,3,2,1,2]
28+
29+
**Output:** 8
30+
31+
**Explanation:**
32+
33+
The minimum distance is achieved by the good tuple `(2, 4, 6)`.
34+
35+
`(2, 4, 6)` is a good tuple because `nums[2] == nums[4] == nums[6] == 2`. Its distance is `abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8`.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [1]
40+
41+
**Output:** \-1
42+
43+
**Explanation:**
44+
45+
There are no good tuples. Therefore, the answer is -1.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
50+
* `1 <= nums[i] <= n`

0 commit comments

Comments
 (0)