Skip to content

Commit d1c7001

Browse files
committed
Added tasks 3701-3704
1 parent 3f1908f commit d1c7001

File tree

12 files changed

+413
-0
lines changed

12 files changed

+413
-0
lines changed
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+
// #Easy #Weekly_Contest_470 #2025_10_06_Time_1_ms_(100.00%)_Space_47.16_MB_(11.11%)
4+
5+
class Solution {
6+
fun alternatingSum(nums: IntArray): Int {
7+
var sum = 0
8+
for (i in nums.indices) {
9+
val num = nums[i]
10+
if (i % 2 == 0) {
11+
sum += num
12+
} else {
13+
sum -= num
14+
}
15+
}
16+
return sum
17+
}
18+
}
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_06_Time_2_ms_(100.00%)_Space_79.41_MB_(83.33%)
4+
5+
class Solution {
6+
fun longestSubsequence(nums: IntArray): Int {
7+
var xorSum = 0
8+
var allZero = true
9+
for (num in nums) {
10+
xorSum = xorSum xor num
11+
if (num != 0) {
12+
allZero = false
13+
}
14+
}
15+
if (allZero) {
16+
return 0
17+
}
18+
return if (xorSum != 0) nums.size else nums.size - 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_06_Time_58_ms_(100.00%)_Space_51.31_MB_(80.00%)
4+
5+
class Solution {
6+
fun removeSubstring(s: String, k: Int): String {
7+
val sb = StringBuilder()
8+
var count = 0
9+
for (ch in s.toCharArray()) {
10+
sb.append(ch)
11+
if (ch == '(') {
12+
count++
13+
} else {
14+
if (count >= k && sb.length >= 2 * k) {
15+
val len = sb.length
16+
var b = true
17+
for (i in len - 2 * k..<len - k) {
18+
if (sb.get(i) != '(') {
19+
b = false
20+
break
21+
}
22+
}
23+
for (i in len - k..<len) {
24+
if (sb.get(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_06_Time_11_ms_(100.00%)_Space_42.73_MB_(100.00%)
4+
5+
class Solution {
6+
fun countNoZeroPairs(n: Long): Long {
7+
var m = 0
8+
var base: Long = 1
9+
while (base <= n) {
10+
m++
11+
base = base * 10
12+
}
13+
val digits = IntArray(m)
14+
var c = n
15+
for (i in 0..<m) {
16+
digits[i] = (c % 10).toInt()
17+
c = c / 10
18+
}
19+
var total: Long = 0
20+
var extra = longArrayOf(1, 0)
21+
base = 1
22+
for (p in 0..<m) {
23+
val nextExtra = longArrayOf(0, 0)
24+
for (e in 0..1) {
25+
for (i in 1..9) {
26+
for (j in 1..9) {
27+
if ((i + j + e) % 10 == digits[p]) {
28+
nextExtra[(i + j + e) / 10] += extra[e]
29+
}
30+
}
31+
}
32+
}
33+
extra = nextExtra
34+
base = base * 10
35+
for (e in 0..1) {
36+
val left = n / base - e
37+
if (left < 0) {
38+
continue
39+
}
40+
if (left == 0L) {
41+
total += extra[e]
42+
} else if (isGood(left)) {
43+
total += 2 * extra[e]
44+
}
45+
}
46+
}
47+
48+
return total
49+
}
50+
51+
private fun isGood(num: Long): Boolean {
52+
var num = num
53+
while (num > 0) {
54+
if (num % 10 == 0L) {
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3701_3800.s3701_compute_alternating_sum
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun alternatingSum() {
10+
assertThat<Int>(
11+
Solution().alternatingSum(intArrayOf(1, 3, 5, 7)),
12+
equalTo<Int>(-4),
13+
)
14+
}
15+
16+
@Test
17+
fun alternatingSum2() {
18+
assertThat<Int>(Solution().alternatingSum(intArrayOf(100)), equalTo<Int>(100))
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3701_3800.s3702_longest_subsequence_with_non_zero_bitwise_xor
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun longestSubsequence() {
10+
assertThat<Int>(
11+
Solution().longestSubsequence(intArrayOf(1, 2, 3)),
12+
equalTo<Int>(2),
13+
)
14+
}
15+
16+
@Test
17+
fun longestSubsequence2() {
18+
assertThat<Int>(
19+
Solution().longestSubsequence(intArrayOf(2, 3, 4)),
20+
equalTo<Int>(3),
21+
)
22+
}
23+
}

0 commit comments

Comments
 (0)