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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3692_majority_frequency_characters;

// #Easy #Biweekly_Contest_166 #2025_09_28_Time_1_ms_(100.00%)_Space_43.06_MB_(100.00%)
// #Easy #String #Hash_Table #Counting #Biweekly_Contest_166
// #2025_10_07_Time_1_ms_(100.00%)_Space_42.89_MB_(97.01%)

public class Solution {
public String majorityFrequencyGroup(String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3693_climbing_stairs_ii;

// #Medium #Biweekly_Contest_166 #2025_09_28_Time_2_ms_(100.00%)_Space_57.06_MB_(100.00%)
// #Medium #Array #Dynamic_Programming #Biweekly_Contest_166
// #2025_10_07_Time_3_ms_(99.74%)_Space_58.06_MB_(96.52%)

@SuppressWarnings({"unused", "java:S1172"})
public class Solution {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3694_distinct_points_reachable_after_substring_removal;

// #Medium #Biweekly_Contest_166 #2025_09_28_Time_37_ms_(100.00%)_Space_46.42_MB_(100.00%)
// #Medium #String #Hash_Table #Prefix_Sum #Sliding_Window #Biweekly_Contest_166
// #2025_10_07_Time_38_ms_(97.74%)_Space_46.84_MB_(98.59%)

import java.util.HashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3695_maximize_alternating_sum_using_swaps;

// #Hard #Biweekly_Contest_166 #2025_09_28_Time_27_ms_(99.82%)_Space_106.96_MB_(100.00%)
// #Hard #Array #Sorting #Greedy #Union_Find #Biweekly_Contest_166
// #2025_10_07_Time_29_ms_(99.13%)_Space_106.44_MB_(52.69%)

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package g3601_3700.s3697_compute_decimal_representation;

// #Easy #Weekly_Contest_469 #2025_09_28_Time_1_ms_(100.00%)_Space_43.24_MB_(25.49%)
// #Easy #Array #Math #Weekly_Contest_469 #2025_10_07_Time_1_ms_(100.00%)_Space_42.84_MB_(83.73%)

public class Solution {
public int[] decimalRepresentation(int n) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3698_split_array_with_minimum_difference;

// #Medium #Weekly_Contest_469 #2025_09_28_Time_2_ms_(99.58%)_Space_62.78_MB_(7.35%)
// #Medium #Array #Prefix_Sum #Weekly_Contest_469
// #2025_10_07_Time_2_ms_(99.48%)_Space_62.63_MB_(12.47%)

public class Solution {
public long splitArray(int[] nums) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3699_number_of_zigzag_arrays_i;

// #Hard #Weekly_Contest_469 #2025_09_28_Time_198_ms_(99.77%)_Space_43.67_MB_(99.32%)
// #Hard #Dynamic_Programming #Prefix_Sum #Weekly_Contest_469
// #2025_10_07_Time_197_ms_(97.34%)_Space_43.41_MB_(92.35%)

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package g3601_3700.s3700_number_of_zigzag_arrays_ii;

// #Hard #Weekly_Contest_469 #2025_09_28_Time_175_ms_(96.34%)_Space_45.46_MB_(28.66%)
// #Hard #Dynamic_Programming #Math #Weekly_Contest_469
// #2025_10_07_Time_173_ms_(92.39%)_Space_44.61_MB_(92.59%)

public class Solution {
public int zigZagArrays(int n, int l, int r) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3701_3800.s3701_compute_alternating_sum;

// #Easy #Weekly_Contest_470 #2025_10_05_Time_1_ms_(100.00%)_Space_44.28_MB_(66.67%)

public class Solution {
public int alternatingSum(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (i % 2 == 0) {
sum += num;
} else {
sum -= num;
}
}
return sum;
}
}
38 changes: 38 additions & 0 deletions src/main/java/g3701_3800/s3701_compute_alternating_sum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3701\. Compute Alternating Sum

Easy

You are given an integer array `nums`.

The **alternating sum** of `nums` is the value obtained by **adding** elements at even indices and **subtracting** elements at odd indices. That is, `nums[0] - nums[1] + nums[2] - nums[3]...`

Return an integer denoting the alternating sum of `nums`.

**Example 1:**

**Input:** nums = [1,3,5,7]

**Output:** \-4

**Explanation:**

* Elements at even indices are `nums[0] = 1` and `nums[2] = 5` because 0 and 2 are even numbers.
* Elements at odd indices are `nums[1] = 3` and `nums[3] = 7` because 1 and 3 are odd numbers.
* The alternating sum is `nums[0] - nums[1] + nums[2] - nums[3] = 1 - 3 + 5 - 7 = -4`.

**Example 2:**

**Input:** nums = [100]

**Output:** 100

**Explanation:**

* The only element at even indices is `nums[0] = 100` because 0 is an even number.
* There are no elements on odd indices.
* The alternating sum is `nums[0] = 100`.

**Constraints:**

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

// #Medium #Weekly_Contest_470 #2025_10_05_Time_2_ms_(100.00%)_Space_64.20_MB_(100.00%)

public class Solution {
public int longestSubsequence(int[] nums) {
int xorSum = 0;
boolean allZero = true;
for (int num : nums) {
xorSum ^= num;
if (num != 0) {
allZero = false;
}
}
if (allZero) {
return 0;
}
return xorSum != 0 ? nums.length : nums.length - 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
3702\. Longest Subsequence With Non-Zero Bitwise XOR

Medium

You are given an integer array `nums`.

Return the length of the **longest subsequence** in `nums` whose bitwise **XOR** is **non-zero**. If no such **subsequence** exists, return 0.

**Example 1:**

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

**Output:** 2

**Explanation:**

One longest subsequence is `[2, 3]`. The bitwise XOR is computed as `2 XOR 3 = 1`, which is non-zero.

**Example 2:**

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

**Output:** 3

**Explanation:**

The longest subsequence is `[2, 3, 4]`. The bitwise XOR is computed as `2 XOR 3 XOR 4 = 5`, which is non-zero.

**Constraints:**

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

// #Medium #Weekly_Contest_470 #2025_10_05_Time_191_ms_(100.00%)_Space_45.86_MB_(100.00%)

public class Solution {
public String removeSubstring(String s, int k) {
StringBuilder sb = new StringBuilder();
int count = 0;
for (char ch : s.toCharArray()) {
sb.append(ch);
if (ch == '(') {
count++;
} else {
if (count >= k && sb.length() >= 2 * k) {
int len = sb.length();
boolean b = true;
for (int i = len - 2 * k; i < len - k; i++) {
if (sb.charAt(i) != '(') {
b = false;
break;
}
}
for (int i = len - k; i < len; i++) {
if (sb.charAt(i) != ')') {
b = false;
break;
}
}
if (b) {
sb.delete(sb.length() - 2 * k, sb.length());
count -= k;
}
}
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
3703\. Remove K-Balanced Substrings

Medium

You are given a string `s` consisting of `'('` and `')'`, and an integer `k`.

A **string** is **k-balanced** if it is **exactly** `k` **consecutive** `'('` followed by `k` **consecutive** `')'`, i.e., `'(' * k + ')' * k`.

For example, if `k = 3`, k-balanced is `"((()))"`.

You must **repeatedly** remove all **non-overlapping k-balanced **substring**** from `s`, and then join the remaining parts. Continue this process until no k-balanced **substring** exists.

Return the final string after all possible removals.

**Example 1:**

**Input:** s = "(())", k = 1

**Output:** ""

**Explanation:**

k-balanced substring is `"()"`

| Step | Current `s` | `k-balanced` | Result `s` |
|------|--------------|--------------------------|------------|
| 1 | `(() )` | `(<s>**()**</s>)` | `()` |
| 2 | `()` | `<s>**()`**</s>` | Empty |

Thus, the final string is `""`.

**Example 2:**

**Input:** s = "(()(", k = 1

**Output:** "(("

**Explanation:**

k-balanced substring is `"()"`

| Step | Current `s` | `k-balanced` | Result `s` |
|------|--------------|----------------------|------------|
| 1 | `(()(` | `(~**()**~)(` | `((` |
| 2 | `((` | - | `((` |

Thus, the final string is `"(("`.

**Example 3:**

**Input:** s = "((()))()()()", k = 3

**Output:** "()()()"

**Explanation:**

k-balanced substring is `"((()))"`

| Step | Current `s` | `k-balanced` | Result `s` |
|------|-------------------|----------------------------------|------------|
| 1 | `((()))()()()` | ~~**((()))**~~`()()()` | `()()()` |
| 2 | `()()()` | - | `()()()` |

Thus, the final string is `"()()()"`.

**Constraints:**

* <code>2 <= s.length <= 10<sup>5</sup></code>
* `s` consists only of `'('` and `')'`.
* `1 <= k <= s.length / 2`
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package g3701_3800.s3704_count_no_zero_pairs_that_sum_to_n;

// #Hard #Weekly_Contest_470 #2025_10_05_Time_4_ms_(97.37%)_Space_41.07_MB_(100.00%)

public class Solution {
public long countNoZeroPairs(long n) {
int m = 0;
long base = 1;
while (base <= n) {
m++;
base = base * 10;
}
int[] digits = new int[m];
long c = n;
for (int i = 0; i < m; i++) {
digits[i] = (int) (c % 10);
c = c / 10;
}
long total = 0;
long[] extra = {1, 0};
base = 1;
for (int p = 0; p < m; p++) {
long[] nextExtra = {0, 0};
for (int e = 0; e <= 1; e++) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if ((i + j + e) % 10 == digits[p]) {
nextExtra[(i + j + e) / 10] += extra[e];
}
}
}
}
extra = nextExtra;
base = base * 10;
for (int e = 0; e <= 1; e++) {
long left = n / base - e;
if (left < 0) {
continue;
}
if (left == 0) {
total += extra[e];
} else if (isGood(left)) {
total += 2 * extra[e];
}
}
}

return total;
}

private boolean isGood(long num) {
while (num > 0) {
if (num % 10 == 0) {
return false;
}
num = num / 10;
}
return true;
}
}
Loading