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_10_03_Time_2_ms_(100.00%)_Space_43.05_MB_(100.00%)
// #Easy #String #Hash_Table #Counting #Biweekly_Contest_166
// #2025_10_03_Time_2_ms_(100.00%)_Space_43.05_MB_(100.00%)

class Solution {
fun majorityFrequencyGroup(s: String): String {
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_10_03_Time_8_ms_(100.00%)_Space_80.61_MB_(12.90%)
// #Medium #Array #Dynamic_Programming #Biweekly_Contest_166
// #2025_10_03_Time_8_ms_(100.00%)_Space_80.61_MB_(12.90%)

import kotlin.math.min

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_10_03_Time_46_ms_(100.00%)_Space_48.62_MB_(100.00%)
// #Medium #String #Hash_Table #Prefix_Sum #Sliding_Window #Biweekly_Contest_166
// #2025_10_03_Time_46_ms_(100.00%)_Space_48.62_MB_(100.00%)

class Solution {
fun distinctPoints(s: String, k: Int): Int {
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_10_03_Time_61_ms_(100.00%)_Space_105.29_MB_(100.00%)
// #Hard #Array #Sorting #Greedy #Union_Find #Biweekly_Contest_166
// #2025_10_03_Time_61_ms_(100.00%)_Space_105.29_MB_(100.00%)

class Solution {
private lateinit var root: IntArray
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_10_03_Time_1_ms_(100.00%)_Space_42.64_MB_(100.00%)
// #Easy #Array #Math #Weekly_Contest_469 #2025_10_03_Time_1_ms_(100.00%)_Space_42.64_MB_(100.00%)

class Solution {
fun decimalRepresentation(n: Int): IntArray {
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_10_03_Time_3_ms_(100.00%)_Space_69.93_MB_(52.17%)
// #Medium #Array #Prefix_Sum #Weekly_Contest_469
// #2025_10_03_Time_3_ms_(100.00%)_Space_69.93_MB_(52.17%)

import kotlin.math.abs
import kotlin.math.min
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_10_03_Time_227_ms_(78.57%)_Space_47.61_MB_(42.86%)
// #Hard #Dynamic_Programming #Prefix_Sum #Weekly_Contest_469
// #2025_10_03_Time_227_ms_(78.57%)_Space_47.61_MB_(42.86%)

class Solution {
fun zigZagArrays(n: Int, l: Int, r: Int): Int {
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_10_03_Time_175_ms_(100.00%)_Space_49.83_MB_(50.00%)
// #Hard #Dynamic_Programming #Math #Weekly_Contest_469
// #2025_10_03_Time_175_ms_(100.00%)_Space_49.83_MB_(50.00%)

class Solution {
fun zigZagArrays(n: Int, l: Int, r: Int): Int {
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_06_Time_1_ms_(100.00%)_Space_47.16_MB_(11.11%)

class Solution {
fun alternatingSum(nums: IntArray): Int {
var sum = 0
for (i in nums.indices) {
val num = nums[i]
if (i % 2 == 0) {
sum += num
} else {
sum -= num
}
}
return sum
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/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_06_Time_2_ms_(100.00%)_Space_79.41_MB_(83.33%)

class Solution {
fun longestSubsequence(nums: IntArray): Int {
var xorSum = 0
var allZero = true
for (num in nums) {
xorSum = xorSum xor num
if (num != 0) {
allZero = false
}
}
if (allZero) {
return 0
}
return if (xorSum != 0) nums.size else nums.size - 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_06_Time_58_ms_(100.00%)_Space_51.31_MB_(80.00%)

class Solution {
fun removeSubstring(s: String, k: Int): String {
val sb = StringBuilder()
var count = 0
for (ch in s.toCharArray()) {
sb.append(ch)
if (ch == '(') {
count++
} else {
if (count >= k && sb.length >= 2 * k) {
val len = sb.length
var b = true
for (i in len - 2 * k..<len - k) {
if (sb[i] != '(') {
b = false
break
}
}
for (i in len - k..<len) {
if (sb[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,61 @@
package g3701_3800.s3704_count_no_zero_pairs_that_sum_to_n

// #Hard #Weekly_Contest_470 #2025_10_06_Time_11_ms_(100.00%)_Space_42.73_MB_(100.00%)

class Solution {
fun countNoZeroPairs(n: Long): Long {
var m = 0
var base: Long = 1
while (base <= n) {
m++
base = base * 10
}
val digits = IntArray(m)
var c = n
for (i in 0..<m) {
digits[i] = (c % 10).toInt()
c = c / 10
}
var total: Long = 0
var extra = longArrayOf(1, 0)
base = 1
for (p in 0..<m) {
val nextExtra = longArrayOf(0, 0)
for (e in 0..1) {
for (i in 1..9) {
for (j in 1..9) {
if ((i + j + e) % 10 == digits[p]) {
nextExtra[(i + j + e) / 10] += extra[e]
}
}
}
}
extra = nextExtra
base = base * 10
for (e in 0..1) {
val left = n / base - e
if (left < 0) {
continue
}
if (left == 0L) {
total += extra[e]
} else if (isGood(left)) {
total += 2 * extra[e]
}
}
}

return total
}

private fun isGood(num: Long): Boolean {
var num = num
while (num > 0) {
if (num % 10 == 0L) {
return false
}
num = num / 10
}
return true
}
}
Loading