Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3701_3800.s3737_count_subarrays_with_majority_element_i;

// #Medium #Array #Hash_Table #Prefix_Sum #Counting #Divide_and_Conquer #Segment_Tree #Merge_Sort
// #Senior #Biweekly_Contest_169 #2026_04_26_Time_1_ms_(100.00%)_Space_46.96_MB_(57.62%)

public class Solution {
public int countMajoritySubarrays(int[] a, int target) {
int n = a.length;
int pre = n + 1;
int res = 0;
int[] count = new int[2 * n + 2];
int[] acc = new int[2 * n + 2];
count[pre] = acc[pre] = 1;
for (int i : a) {
pre += (i == target ? 1 : -1);
count[pre]++;
acc[pre] = acc[pre - 1] + count[pre];
res += acc[pre - 1];
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3737\. Count Subarrays With Majority Element I

Medium

You are given an integer array `nums` and an integer `target`.

Return the number of **non-empty subarrays** of `nums` in which `target` is the **majority element**.

The **majority element** of a subarray is the element that appears **strictly** **more than half** of the times in that subarray.

**Example 1:**

**Input:** nums = [1,2,2,3], target = 2

**Output:** 5

**Explanation:**

Valid subarrays with `target = 2` as the majority element:

* `nums[1..1] = [2]`
* `nums[2..2] = [2]`
* `nums[1..2] = [2,2]`
* `nums[0..2] = [1,2,2]`
* `nums[1..3] = [2,2,3]`

So there are 5 such subarrays.

**Example 2:**

**Input:** nums = [1,1,1,1], target = 1

**Output:** 10

**Explanation:**

All 10 subarrays have 1 as the majority element.

**Example 3:**

**Input:** nums = [1,2,3], target = 4

**Output:** 0

**Explanation:**

`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.

**Constraints:**

* `1 <= nums.length <= 1000`
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= target <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g3701_3800.s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element;

// #Medium #Array #Dynamic_Programming #Staff #Biweekly_Contest_169
// #2026_04_26_Time_7_ms_(99.28%)_Space_123.88_MB_(5.07%)

public class Solution {
public int longestSubarray(int[] nums) {
int n = nums.length;
if (n < 3) {
return n;
}
int i = 0;
int len1 = 0;
int len2 = 0;
int ans = 2;
while (i < n) {
int l = (i == 0) ? -1000 * 1000 * 10 : nums[i - 1];
int r = (i == n - 1) ? 1000 * 1000 * 10 : nums[i + 1];
if (l <= nums[i]) {
len2++;
ans = Math.max(len2 + 1, ans);
} else if (r >= nums[i]) {
int j = i;
len1 = len2;
while (j < n - 1 && nums[j] <= nums[j + 1]) {
j++;
}
len2 = j - i + 1;
ans = Math.max(len2 + 1, ans);
if (l <= r || (len1 > 1 && nums[i - 2] <= nums[i])) {
ans = Math.max(len1 + len2, ans);
}
i = j;
} else {
len2 = 0;
}
i++;
}
ans = Math.min(ans, n);
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3738\. Longest Non-Decreasing Subarray After Replacing at Most One Element

Medium

You are given an integer array `nums`.

You are allowed to replace **at most** one element in the array with any other integer value of your choice.

Return the length of the **longest non-decreasing subarray** that can be obtained after performing at most one replacement.

An array is said to be **non-decreasing** if each element is greater than or equal to its previous one (if it exists).

**Example 1:**

**Input:** nums = [1,2,3,1,2]

**Output:** 4

**Explanation:**

Replacing `nums[3] = 1` with 3 gives the array [1, 2, 3, 3, 2].

The longest non-decreasing subarray is [1, 2, 3, 3], which has a length of 4.

**Example 2:**

**Input:** nums = [2,2,2,2,2]

**Output:** 5

**Explanation:**

All elements in `nums` are equal, so it is already non-decreasing and the entire `nums` forms a subarray of length 5.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g3701_3800.s3739_count_subarrays_with_majority_element_ii;

// #Hard #Array #Hash_Table #Prefix_Sum #Divide_and_Conquer #Segment_Tree #Merge_Sort #Senior_Staff
// #Biweekly_Contest_169 #2026_04_26_Time_3_ms_(100.00%)_Space_90.94_MB_(36.19%)

public class Solution {
public long countMajoritySubarrays(int[] nums, int target) {
int n = nums.length;
int pre = n + 1;
long[] count = new long[2 * n + 2];
long[] acc = new long[2 * n + 2];
long res = 0;
count[pre] = acc[pre] = 1;
for (int a : nums) {
pre += (a == target ? 1 : -1);
acc[pre] = ++count[pre] + acc[pre - 1];
res += acc[pre - 1];
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3739\. Count Subarrays With Majority Element II

Hard

You are given an integer array `nums` and an integer `target`.

Return the number of **non-empty subarrays** of `nums` in which `target` is the **majority element**.

The **majority element** of a subarray is the element that appears **strictly more than half** of the times in that subarray.

**Example 1:**

**Input:** nums = [1,2,2,3], target = 2

**Output:** 5

**Explanation:**

Valid subarrays with `target = 2` as the majority element:

* `nums[1..1] = [2]`
* `nums[2..2] = [2]`
* `nums[1..2] = [2,2]`
* `nums[0..2] = [1,2,2]`
* `nums[1..3] = [2,2,3]`

So there are 5 such subarrays.

**Example 2:**

**Input:** nums = [1,1,1,1], target = 1

**Output:** 10

**Explanation:**

All 10 subarrays have 1 as the majority element.

**Example 3:**

**Input:** nums = [1,2,3], target = 4

**Output:** 0

**Explanation:**

`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.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= target <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g3701_3800.s3740_minimum_distance_between_three_equal_elements_i;

// #Easy #Array #Hash_Table #Mid_Level #Weekly_Contest_475
// #2026_04_26_Time_1_ms_(99.99%)_Space_44.21_MB_(75.36%)

public class Solution {
public int minimumDistance(int[] nums) {
int len = nums.length;
int[] last2 = new int[len];
int res = 200;
for (int i = 0; i < len; i++) {
int val = nums[i] - 1;
int pos = i + 1;
int pack = last2[val];
int old = pack & 255;
int cur = pack >> 8;
last2[val] = cur | (pos << 8);
if (old > 0) {
res = Math.min(res, (pos - old) << 1);
}
}
return res == 200 ? -1 : res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3740\. Minimum Distance Between Three Equal Elements I

Easy

You are given an integer array `nums`.

A tuple `(i, j, k)` of 3 **distinct** indices is **good** if `nums[i] == nums[j] == nums[k]`.

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`.

Return an integer denoting the **minimum** possible **distance** of a **good** tuple. If no **good** tuples exist, return `-1`.

**Example 1:**

**Input:** nums = [1,2,1,1,3]

**Output:** 6

**Explanation:**

The minimum distance is achieved by the good tuple `(0, 2, 3)`.

`(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`.

**Example 2:**

**Input:** nums = [1,1,2,3,2,1,2]

**Output:** 8

**Explanation:**

The minimum distance is achieved by the good tuple `(2, 4, 6)`.

`(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`.

**Example 3:**

**Input:** nums = [1]

**Output:** \-1

**Explanation:**

There are no good tuples. Therefore, the answer is -1.

**Constraints:**

* `1 <= n == nums.length <= 100`
* `1 <= nums[i] <= n`
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3701_3800.s3741_minimum_distance_between_three_equal_elements_ii;

// #Medium #Array #Hash_Table #Senior #Weekly_Contest_475
// #2026_04_26_Time_6_ms_(99.60%)_Space_161.24_MB_(96.23%)

public class Solution {
public int minimumDistance(int[] nums) {
int n = nums.length;
int ans = Integer.MAX_VALUE;
int[] prev1 = new int[n + 1];
int[] prev2 = new int[n + 1];
for (int i = 0; i < n + 1; i++) {
prev1[i] = prev2[i] = -1;
}
for (int i = 0; i < n; i++) {
int value = nums[i];
if (prev2[value] != -1) {
ans = Math.min(ans, (i - prev2[value]));
}
prev2[value] = prev1[value];
prev1[value] = i;
}
if (ans < 100002) {
return ans * 2;
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3741\. Minimum Distance Between Three Equal Elements II

Medium

You are given an integer array `nums`.

A tuple `(i, j, k)` of 3 **distinct** indices is **good** if `nums[i] == nums[j] == nums[k]`.

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`.

Return an integer denoting the **minimum** possible **distance** of a **good** tuple. If no **good** tuples exist, return `-1`.

**Example 1:**

**Input:** nums = [1,2,1,1,3]

**Output:** 6

**Explanation:**

The minimum distance is achieved by the good tuple `(0, 2, 3)`.

`(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`.

**Example 2:**

**Input:** nums = [1,1,2,3,2,1,2]

**Output:** 8

**Explanation:**

The minimum distance is achieved by the good tuple `(2, 4, 6)`.

`(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`.

**Example 3:**

**Input:** nums = [1]

**Output:** \-1

**Explanation:**

There are no good tuples. Therefore, the answer is -1.

**Constraints:**

* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
* `1 <= nums[i] <= n`
Loading
Loading