From 6f370c25f9f6dc18c5f71136a8f2544c7053d9a7 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 27 Jul 2025 17:26:18 +0300 Subject: [PATCH 1/3] Added tasks 3627-3630 --- .../Solution.kt | 18 ++++ .../readme.md | 43 +++++++++ .../Solution.kt | 47 ++++++++++ .../readme.md | 44 ++++++++++ .../Solution.kt | 87 +++++++++++++++++++ .../readme.md | 59 +++++++++++++ .../Solution.kt | 43 +++++++++ .../readme.md | 71 +++++++++++++++ .../SolutionTest.kt | 23 +++++ .../SolutionTest.kt | 22 +++++ .../SolutionTest.kt | 42 +++++++++ .../SolutionTest.kt | 28 ++++++ 12 files changed, 527 insertions(+) create mode 100644 src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/readme.md create mode 100644 src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/Solution.kt create mode 100644 src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/readme.md create mode 100644 src/test/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/SolutionTest.kt create mode 100644 src/test/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/SolutionTest.kt diff --git a/src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/Solution.kt b/src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/Solution.kt new file mode 100644 index 00000000..58b3f107 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/Solution.kt @@ -0,0 +1,18 @@ +package g3601_3700.s3627_maximum_median_sum_of_subsequences_of_size_3 + +// #Medium #Weekly_Contest_460 #2025_07_27_Time_46_ms_(91.67%)_Space_133.87_MB_(16.67%) + +class Solution { + fun maximumMedianSum(nums: IntArray): Long { + nums.sort() + var i = 0 + var j = nums.size + var sum = 0L + while (i < j) { + sum += nums[j - 2] + j = j - 2 + i++ + } + return sum + } +} diff --git a/src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/readme.md b/src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/readme.md new file mode 100644 index 00000000..78578f28 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/readme.md @@ -0,0 +1,43 @@ +3627\. Maximum Median Sum of Subsequences of Size 3 + +Medium + +You are given an integer array `nums` with a length divisible by 3. + +You want to make the array empty in steps. In each step, you can select any three elements from the array, compute their **median**, and remove the selected elements from the array. + +The **median** of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order. + +Return the **maximum** possible sum of the medians computed from the selected elements. + +**Example 1:** + +**Input:** nums = [2,1,3,2,1,3] + +**Output:** 5 + +**Explanation:** + +* In the first step, select elements at indices 2, 4, and 5, which have a median 3. After removing these elements, `nums` becomes `[2, 1, 2]`. +* In the second step, select elements at indices 0, 1, and 2, which have a median 2. After removing these elements, `nums` becomes empty. + +Hence, the sum of the medians is `3 + 2 = 5`. + +**Example 2:** + +**Input:** nums = [1,1,10,10,10,10] + +**Output:** 20 + +**Explanation:** + +* In the first step, select elements at indices 0, 2, and 3, which have a median 10. After removing these elements, `nums` becomes `[1, 10, 10]`. +* In the second step, select elements at indices 0, 1, and 2, which have a median 10. After removing these elements, `nums` becomes empty. + +Hence, the sum of the medians is `10 + 10 = 20`. + +**Constraints:** + +* 1 <= nums.length <= 5 * 105 +* `nums.length % 3 == 0` +* 1 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/Solution.kt b/src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/Solution.kt new file mode 100644 index 00000000..ea855753 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/Solution.kt @@ -0,0 +1,47 @@ +package g3601_3700.s3628_maximum_number_of_subsequences_after_one_inserting + +// #Medium #Weekly_Contest_460 #2025_07_27_Time_13_ms_(100.00%)_Space_48.00_MB_(75.00%) + +class Solution { + fun numOfSubsequences(s: String): Long { + var tc: Long = 0 + val chs = s.toCharArray() + for (c in chs) { + tc += (if (c == 'T') 1 else 0).toLong() + } + var ls: Long = 0 + var cs: Long = 0 + var lcf: Long = 0 + var ctf: Long = 0 + var lct: Long = 0 + var ocg: Long = 0 + var tp: Long = 0 + for (curr in chs) { + val rt = tc - tp + val cg = ls * rt + ocg = if (cg > ocg) cg else ocg + if (curr == 'L') { + ls++ + } else { + if (curr == 'C') { + cs++ + lcf += ls + } else { + if (curr == 'T') { + lct += lcf + ctf += cs + tp++ + } + } + } + } + val fcg = ls * (tc - tp) + ocg = if (fcg > ocg) fcg else ocg + var maxi: Long = 0 + val bo = longArrayOf(lcf, ctf, ocg) + for (op in bo) { + maxi = if (op > maxi) op else maxi + } + return lct + maxi + } +} diff --git a/src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/readme.md b/src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/readme.md new file mode 100644 index 00000000..baf68074 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/readme.md @@ -0,0 +1,44 @@ +3628\. Maximum Number of Subsequences After One Inserting + +Medium + +You are given a string `s` consisting of uppercase English letters. + +You are allowed to insert **at most one** uppercase English letter at **any** position (including the beginning or end) of the string. + +Return the **maximum** number of `"LCT"` subsequences that can be formed in the resulting string after **at most one insertion**. + +**Example 1:** + +**Input:** s = "LMCT" + +**Output:** 2 + +**Explanation:** + +We can insert a `"L"` at the beginning of the string s to make `"LLMCT"`, which has 2 subsequences, at indices [0, 3, 4] and [1, 3, 4]. + +**Example 2:** + +**Input:** s = "LCCT" + +**Output:** 4 + +**Explanation:** + +We can insert a `"L"` at the beginning of the string s to make `"LLCCT"`, which has 4 subsequences, at indices [0, 2, 4], [0, 3, 4], [1, 2, 4] and [1, 3, 4]. + +**Example 3:** + +**Input:** s = "L" + +**Output:** 0 + +**Explanation:** + +Since it is not possible to obtain the subsequence `"LCT"` by inserting a single letter, the result is 0. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` consists of uppercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/Solution.kt b/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/Solution.kt new file mode 100644 index 00000000..7df7e4ac --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/Solution.kt @@ -0,0 +1,87 @@ +package g3601_3700.s3629_minimum_jumps_to_reach_end_via_prime_teleportation + +// #Medium #Weekly_Contest_460 #2025_07_27_Time_406_ms_(100.00%)_Space_153.64_MB_(100.00%) + +import java.util.ArrayDeque +import kotlin.math.max + +class Solution { + fun minJumps(nums: IntArray): Int { + val n = nums.size + if (n == 1) { + return 0 + } + var maxVal = 0 + for (v in nums) { + maxVal = max(maxVal, v) + } + val isPrime = sieve(maxVal) + val posOfValue: Array> = Array>(maxVal + 1) { ArrayList() } + for (i in 0..() + q.add(0) + dist[0] = 0 + while (!q.isEmpty()) { + val i: Int = q.poll()!! + val d = dist[i] + if (i == n - 1) { + return d + } + if (i + 1 < n && dist[i + 1] == -1) { + dist[i + 1] = d + 1 + q.add(i + 1) + } + if (i - 1 >= 0 && dist[i - 1] == -1) { + dist[i - 1] = d + 1 + q.add(i - 1) + } + val v = nums[i] + if (v <= maxVal && isPrime[v] && !primeProcessed[v]) { + var mult = v + while (mult <= maxVal) { + val list = posOfValue[mult] + for (idx in list) { + if (dist[idx] == -1) { + dist[idx] = d + 1 + q.add(idx) + } + } + mult += v + } + primeProcessed[v] = true + } + } + return -1 + } + + private fun sieve(n: Int): BooleanArray { + val prime = BooleanArray(n + 1) + if (n >= 2) { + prime.fill(true) + } + if (n >= 0) { + prime[0] = false + } + if (n >= 1) { + prime[1] = false + } + var i = 2 + while (i.toLong() * i <= n) { + if (prime[i]) { + var j = i * i + while (j <= n) { + prime[j] = false + j += i + } + } + i++ + } + return prime + } +} diff --git a/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/readme.md b/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/readme.md new file mode 100644 index 00000000..6a4a54f1 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/readme.md @@ -0,0 +1,59 @@ +3629\. Minimum Jumps to Reach End via Prime Teleportation + +Medium + +You are given an integer array `nums` of length `n`. + +You start at index 0, and your goal is to reach index `n - 1`. + +From any index `i`, you may perform one of the following operations: + +* **Adjacent Step**: Jump to index `i + 1` or `i - 1`, if the index is within bounds. +* **Prime Teleportation**: If `nums[i]` is a prime number `p`, you may instantly jump to any index `j != i` such that `nums[j] % p == 0`. + +Return the **minimum** number of jumps required to reach index `n - 1`. + +**Example 1:** + +**Input:** nums = [1,2,4,6] + +**Output:** 2 + +**Explanation:** + +One optimal sequence of jumps is: + +* Start at index `i = 0`. Take an adjacent step to index 1. +* At index `i = 1`, `nums[1] = 2` is a prime number. Therefore, we teleport to index `i = 3` as `nums[3] = 6` is divisible by 2. + +Thus, the answer is 2. + +**Example 2:** + +**Input:** nums = [2,3,4,7,9] + +**Output:** 2 + +**Explanation:** + +One optimal sequence of jumps is: + +* Start at index `i = 0`. Take an adjacent step to index `i = 1`. +* At index `i = 1`, `nums[1] = 3` is a prime number. Therefore, we teleport to index `i = 4` since `nums[4] = 9` is divisible by 3. + +Thus, the answer is 2. + +**Example 3:** + +**Input:** nums = [4,6,5,8] + +**Output:** 3 + +**Explanation:** + +* Since no teleportation is possible, we move through `0 → 1 → 2 → 3`. Thus, the answer is 3. + +**Constraints:** + +* 1 <= n == nums.length <= 105 +* 1 <= nums[i] <= 106 \ No newline at end of file diff --git a/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/Solution.kt b/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/Solution.kt new file mode 100644 index 00000000..509a49cf --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/Solution.kt @@ -0,0 +1,43 @@ +package g3601_3700.s3630_partition_array_for_maximum_xor_and_and + +// #Hard #Weekly_Contest_460 #2025_07_27_Time_72_ms_(100.00%)_Space_52.17_MB_(100.00%) + +class Solution { + fun maximizeXorAndXor(nums: IntArray): Long { + val n = nums.size + val full = 1 shl n + val xorMask = IntArray(full) + val andMask = IntArray(full) + val orMask = IntArray(full) + for (mask in 1.. best) { + best = sum + } + if (a == 0) { + break + } + a = (a - 1) and rest + } + } + return best + } +} diff --git a/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/readme.md b/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/readme.md new file mode 100644 index 00000000..4bcfa6e7 --- /dev/null +++ b/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/readme.md @@ -0,0 +1,71 @@ +3630\. Partition Array for Maximum XOR and AND + +Hard + +You are given an integer array `nums`. + +Partition the array into **three** (possibly empty) **subsequences** `A`, `B`, and `C` such that every element of `nums` belongs to **exactly** one subsequence. + +Your goal is to **maximize** the value of: `XOR(A) + AND(B) + XOR(C)` + +where: + +* `XOR(arr)` denotes the bitwise XOR of all elements in `arr`. If `arr` is empty, its value is defined as 0. +* `AND(arr)` denotes the bitwise AND of all elements in `arr`. If `arr` is empty, its value is defined as 0. + +Return the **maximum** value achievable. + +**Note:** If multiple partitions result in the same **maximum** sum, you can consider any one of them. + +**Example 1:** + +**Input:** nums = [2,3] + +**Output:** 5 + +**Explanation:** + +One optimal partition is: + +* `A = [3], XOR(A) = 3` +* `B = [2], AND(B) = 2` +* `C = [], XOR(C) = 0` + +The maximum value of: `XOR(A) + AND(B) + XOR(C) = 3 + 2 + 0 = 5`. Thus, the answer is 5. + +**Example 2:** + +**Input:** nums = [1,3,2] + +**Output:** 6 + +**Explanation:** + +One optimal partition is: + +* `A = [1], XOR(A) = 1` +* `B = [2], AND(B) = 2` +* `C = [3], XOR(C) = 3` + +The maximum value of: `XOR(A) + AND(B) + XOR(C) = 1 + 2 + 3 = 6`. Thus, the answer is 6. + +**Example 3:** + +**Input:** nums = [2,3,6,7] + +**Output:** 15 + +**Explanation:** + +One optimal partition is: + +* `A = [7], XOR(A) = 7` +* `B = [2,3], AND(B) = 2` +* `C = [6], XOR(C) = 6` + +The maximum value of: `XOR(A) + AND(B) + XOR(C) = 7 + 2 + 6 = 15`. Thus, the answer is 15. + +**Constraints:** + +* `1 <= nums.length <= 19` +* 1 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/test/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/SolutionTest.kt new file mode 100644 index 00000000..f4b64bf2 --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3601_3700.s3627_maximum_median_sum_of_subsequences_of_size_3 + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumMedianSum() { + assertThat( + Solution().maximumMedianSum(intArrayOf(2, 1, 3, 2, 1, 3)), + equalTo(5L), + ) + } + + @Test + fun maximumMedianSum2() { + assertThat( + Solution().maximumMedianSum(intArrayOf(1, 1, 10, 10, 10, 10)), + equalTo(20L), + ) + } +} diff --git a/src/test/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/SolutionTest.kt new file mode 100644 index 00000000..97de7bbd --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3601_3700.s3628_maximum_number_of_subsequences_after_one_inserting + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun numOfSubsequences() { + assertThat(Solution().numOfSubsequences("LMCT"), equalTo(2L)) + } + + @Test + fun numOfSubsequences2() { + assertThat(Solution().numOfSubsequences("LCCT"), equalTo(4L)) + } + + @Test + fun numOfSubsequences3() { + assertThat(Solution().numOfSubsequences("L"), equalTo(0L)) + } +} diff --git a/src/test/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/SolutionTest.kt new file mode 100644 index 00000000..9ee1fa19 --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/SolutionTest.kt @@ -0,0 +1,42 @@ +package g3601_3700.s3629_minimum_jumps_to_reach_end_via_prime_teleportation + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minJumps() { + assertThat(Solution().minJumps(intArrayOf(1, 2, 4, 6)), equalTo(2)) + } + + @Test + fun minJumps2() { + assertThat(Solution().minJumps(intArrayOf(2, 3, 4, 7, 9)), equalTo(2)) + } + + @Test + fun minJumps3() { + assertThat(Solution().minJumps(intArrayOf(4, 6, 5, 8)), equalTo(3)) + } + + @Test + fun minJumps4() { + assertThat( + Solution() + .minJumps( + intArrayOf( + 893, 786, 607, 137, 69, 381, 790, 233, 15, 42, 7, 764, 890, 269, + 84, 262, 870, 514, 514, 650, 269, 485, 760, 181, 489, 107, 585, + 428, 862, 563, + ), + ), + equalTo(21), + ) + } + + @Test + fun minJumps5() { + assertThat(Solution().minJumps(intArrayOf(4)), equalTo(0)) + } +} diff --git a/src/test/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/SolutionTest.kt b/src/test/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/SolutionTest.kt new file mode 100644 index 00000000..f055e353 --- /dev/null +++ b/src/test/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3601_3700.s3630_partition_array_for_maximum_xor_and_and + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximizeXorAndXor() { + assertThat(Solution().maximizeXorAndXor(intArrayOf(2, 3)), equalTo(5L)) + } + + @Test + fun maximizeXorAndXor2() { + assertThat( + Solution().maximizeXorAndXor(intArrayOf(1, 3, 2)), + equalTo(6L), + ) + } + + @Test + fun maximizeXorAndXor3() { + assertThat( + Solution().maximizeXorAndXor(intArrayOf(2, 3, 6, 7)), + equalTo(15L), + ) + } +} From 68c695f934c76dcf003bdbeaed5b8535f4bfeeef Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 27 Jul 2025 17:33:34 +0300 Subject: [PATCH 2/3] Fixed sonar --- .../Solution.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/Solution.kt b/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/Solution.kt index 7df7e4ac..6d0a0ca4 100644 --- a/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation/Solution.kt @@ -27,7 +27,7 @@ class Solution { val q = ArrayDeque() q.add(0) dist[0] = 0 - while (!q.isEmpty()) { + while (q.isNotEmpty()) { val i: Int = q.poll()!! val d = dist[i] if (i == n - 1) { From b82185f20c7f29bd345a5a6cbeba172c9262380f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 3 Aug 2025 07:36:20 +0300 Subject: [PATCH 3/3] Improved tag --- .../s3630_partition_array_for_maximum_xor_and_and/Solution.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/Solution.kt b/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/Solution.kt index 509a49cf..e85c2f97 100644 --- a/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/Solution.kt +++ b/src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and/Solution.kt @@ -1,6 +1,7 @@ package g3601_3700.s3630_partition_array_for_maximum_xor_and_and -// #Hard #Weekly_Contest_460 #2025_07_27_Time_72_ms_(100.00%)_Space_52.17_MB_(100.00%) +// #Hard #Array #Math #Greedy #Enumeration #Weekly_Contest_460 +// #2025_08_03_Time_57_ms_(100.00%)_Space_51.81_MB_(100.00%) class Solution { fun maximizeXorAndXor(nums: IntArray): Long {