Skip to content

Commit 7a692e2

Browse files
committed
Added tasks 3701-3704
1 parent ed03e05 commit 7a692e2

File tree

12 files changed

+402
-0
lines changed

12 files changed

+402
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3701_3800.s3701_compute_alternating_sum;
2+
3+
// #Easy #Weekly_Contest_470 #2025_10_05_Time_1_ms_(100.00%)_Space_44.28_MB_(66.67%)
4+
5+
public class Solution {
6+
public int alternatingSum(int[] nums) {
7+
int sum = 0;
8+
for (int i = 0; i < nums.length; i++) {
9+
int num = nums[i];
10+
if (i % 2 == 0) sum += num;
11+
else sum -= num;
12+
}
13+
return sum;
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3701\. Compute Alternating Sum
2+
3+
Easy
4+
5+
You are given an integer array `nums`.
6+
7+
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]...`
8+
9+
Return an integer denoting the alternating sum of `nums`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,3,5,7]
14+
15+
**Output:** \-4
16+
17+
**Explanation:**
18+
19+
* Elements at even indices are `nums[0] = 1` and `nums[2] = 5` because 0 and 2 are even numbers.
20+
* Elements at odd indices are `nums[1] = 3` and `nums[3] = 7` because 1 and 3 are odd numbers.
21+
* The alternating sum is `nums[0] - nums[1] + nums[2] - nums[3] = 1 - 3 + 5 - 7 = -4`.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [100]
26+
27+
**Output:** 100
28+
29+
**Explanation:**
30+
31+
* The only element at even indices is `nums[0] = 100` because 0 is an even number.
32+
* There are no elements on odd indices.
33+
* The alternating sum is `nums[0] = 100`.
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 100`
38+
* `1 <= nums[i] <= 100`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3701_3800.s3702_longest_subsequence_with_non_zero_bitwise_xor;
2+
3+
// #Medium #Weekly_Contest_470 #2025_10_05_Time_2_ms_(100.00%)_Space_64.20_MB_(100.00%)
4+
5+
public class Solution {
6+
public int longestSubsequence(int[] nums) {
7+
int xorSum = 0;
8+
boolean allZero = true;
9+
for (int num : nums) {
10+
xorSum ^= num;
11+
if (num != 0) {
12+
allZero = false;
13+
}
14+
}
15+
if (allZero) {
16+
return 0;
17+
}
18+
return xorSum != 0 ? nums.length : nums.length - 1;
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3702\. Longest Subsequence With Non-Zero Bitwise XOR
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
Return the length of the **longest subsequence** in `nums` whose bitwise **XOR** is **non-zero**. If no such **subsequence** exists, return 0.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3]
12+
13+
**Output:** 2
14+
15+
**Explanation:**
16+
17+
One longest subsequence is `[2, 3]`. The bitwise XOR is computed as `2 XOR 3 = 1`, which is non-zero.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [2,3,4]
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
The longest subsequence is `[2, 3, 4]`. The bitwise XOR is computed as `2 XOR 3 XOR 4 = 5`, which is non-zero.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
32+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3701_3800.s3703_remove_k_balanced_substrings;
2+
3+
// #Medium #Weekly_Contest_470 #2025_10_05_Time_191_ms_(100.00%)_Space_45.86_MB_(100.00%)
4+
5+
public class Solution {
6+
public String removeSubstring(String s, int k) {
7+
StringBuilder sb = new StringBuilder();
8+
int count = 0;
9+
for (char ch : s.toCharArray()) {
10+
sb.append(ch);
11+
if (ch == '(') {
12+
count++;
13+
} else {
14+
if (count >= k && sb.length() >= 2 * k) {
15+
int len = sb.length();
16+
boolean b = true;
17+
for (int i = len - 2 * k; i < len - k; i++) {
18+
if (sb.charAt(i) != '(') {
19+
b = false;
20+
break;
21+
}
22+
}
23+
for (int i = len - k; i < len; i++) {
24+
if (sb.charAt(i) != ')') {
25+
b = false;
26+
break;
27+
}
28+
}
29+
if (b) {
30+
sb.delete(sb.length() - 2 * k, sb.length());
31+
count -= k;
32+
}
33+
}
34+
}
35+
}
36+
return sb.toString();
37+
}
38+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
3703\. Remove K-Balanced Substrings
2+
3+
Medium
4+
5+
You are given a string `s` consisting of `'('` and `')'`, and an integer `k`.
6+
7+
A **string** is **k-balanced** if it is **exactly** `k` **consecutive** `'('` followed by `k` **consecutive** `')'`, i.e., `'(' * k + ')' * k`.
8+
9+
For example, if `k = 3`, k-balanced is `"((()))"`.
10+
11+
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.
12+
13+
Return the final string after all possible removals.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "(())", k = 1
18+
19+
**Output:** ""
20+
21+
**Explanation:**
22+
23+
k-balanced substring is `"()"`
24+
25+
| Step | Current `s` | `k-balanced` | Result `s` |
26+
|------|--------------|--------------------------|------------|
27+
| 1 | `(() )` | `(<s>**()**</s>)` | `()` |
28+
| 2 | `()` | `<s>**()`**</s>` | Empty |
29+
30+
Thus, the final string is `""`.
31+
32+
**Example 2:**
33+
34+
**Input:** s = "(()(", k = 1
35+
36+
**Output:** "(("
37+
38+
**Explanation:**
39+
40+
k-balanced substring is `"()"`
41+
42+
| Step | Current `s` | `k-balanced` | Result `s` |
43+
|------|--------------|----------------------|------------|
44+
| 1 | `(()(` | `(~**()**~)(` | `((` |
45+
| 2 | `((` | - | `((` |
46+
47+
Thus, the final string is `"(("`.
48+
49+
**Example 3:**
50+
51+
**Input:** s = "((()))()()()", k = 3
52+
53+
**Output:** "()()()"
54+
55+
**Explanation:**
56+
57+
k-balanced substring is `"((()))"`
58+
59+
| Step | Current `s` | `k-balanced` | Result `s` |
60+
|------|-------------------|----------------------------------|------------|
61+
| 1 | `((()))()()()` | ~~**((()))**~~`()()()` | `()()()` |
62+
| 2 | `()()()` | - | `()()()` |
63+
64+
Thus, the final string is `"()()()"`.
65+
66+
**Constraints:**
67+
68+
* <code>2 <= s.length <= 10<sup>5</sup></code>
69+
* `s` consists only of `'('` and `')'`.
70+
* `1 <= k <= s.length / 2`
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g3701_3800.s3704_count_no_zero_pairs_that_sum_to_n;
2+
3+
// #Hard #Weekly_Contest_470 #2025_10_05_Time_4_ms_(97.37%)_Space_41.07_MB_(100.00%)
4+
5+
public class Solution {
6+
public long countNoZeroPairs(long n) {
7+
int m = 0;
8+
long base = 1;
9+
while (base <= n) {
10+
m++;
11+
base = base * 10;
12+
}
13+
int[] digits = new int[m];
14+
long c = n;
15+
for (int i = 0; i < m; i++) {
16+
digits[i] = (int) (c % 10);
17+
c = c / 10;
18+
}
19+
20+
long total = 0;
21+
long[] extra = {1, 0};
22+
base = 1;
23+
for (int p = 0; p < m; p++) {
24+
long[] nextExtra = {0, 0};
25+
for (int e = 0; e <= 1; e++) {
26+
for (int i = 1; i <= 9; i++) {
27+
for (int j = 1; j <= 9; j++) {
28+
if ((i + j + e) % 10 == digits[p]) {
29+
nextExtra[(i + j + e) / 10] += extra[e];
30+
}
31+
}
32+
}
33+
}
34+
extra = nextExtra;
35+
base = base * 10;
36+
for (int e = 0; e <= 1; e++) {
37+
long left = n / base - e;
38+
if (left < 0) {
39+
continue;
40+
}
41+
if (left == 0) {
42+
total += extra[e];
43+
} else if (isGood(left)) {
44+
total += 2 * extra[e];
45+
}
46+
}
47+
}
48+
49+
return total;
50+
}
51+
52+
private boolean isGood(long num) {
53+
while (num > 0) {
54+
if (num % 10 == 0) {
55+
return false;
56+
}
57+
num = num / 10;
58+
}
59+
return true;
60+
}
61+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3704\. Count No-Zero Pairs That Sum to N
2+
3+
Hard
4+
5+
A **no-zero** integer is a **positive** integer that **does not contain the digit** 0 in its decimal representation.
6+
7+
Given an integer `n`, count the number of pairs `(a, b)` where:
8+
9+
* `a` and `b` are **no-zero** integers.
10+
* `a + b = n`
11+
12+
Return an integer denoting the number of such pairs.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 2
17+
18+
**Output:** 1
19+
20+
**Explanation:**
21+
22+
The only pair is `(1, 1)`.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 3
27+
28+
**Output:** 2
29+
30+
**Explanation:**
31+
32+
The pairs are `(1, 2)` and `(2, 1)`.
33+
34+
**Example 3:**
35+
36+
**Input:** n = 11
37+
38+
**Output:** 8
39+
40+
**Explanation:**
41+
42+
The pairs are `(2, 9)`, `(3, 8)`, `(4, 7)`, `(5, 6)`, `(6, 5)`, `(7, 4)`, `(8, 3)`, and `(9, 2)`. Note that `(1, 10)` and `(10, 1)` do not satisfy the conditions because 10 contains 0 in its decimal representation.
43+
44+
**Constraints:**
45+
46+
* <code>2 <= n <= 10<sup>15</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3701_3800.s3701_compute_alternating_sum;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void alternatingSum() {
11+
assertThat(new Solution().alternatingSum(new int[] {1, 3, 5, 7}), equalTo(-4));
12+
}
13+
14+
@Test
15+
void alternatingSum2() {
16+
assertThat(new Solution().alternatingSum(new int[] {100}), equalTo(100));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3701_3800.s3702_longest_subsequence_with_non_zero_bitwise_xor;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void longestSubsequence() {
11+
assertThat(new Solution().longestSubsequence(new int[] {1, 2, 3}), equalTo(2));
12+
}
13+
14+
@Test
15+
void longestSubsequence2() {
16+
assertThat(new Solution().longestSubsequence(new int[] {2, 3, 4}), equalTo(3));
17+
}
18+
}

0 commit comments

Comments
 (0)