From c7b9201bfedaa3594bacf5039ce1125f3b961e57 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Oct 2025 18:35:39 +0200 Subject: [PATCH 1/6] Added tasks 3722-3729 --- .../Solution.java | 70 ++++++++++++++++++ .../readme.md | 52 ++++++++++++++ .../Solution.java | 27 +++++++ .../readme.md | 60 ++++++++++++++++ .../Solution.java | 28 ++++++++ .../readme.md | 71 +++++++++++++++++++ .../Solution.java | 51 +++++++++++++ .../readme.md | 42 +++++++++++ .../Solution.java | 16 +++++ .../readme.md | 31 ++++++++ .../Solution.java | 34 +++++++++ .../readme.md | 44 ++++++++++++ .../Solution.java | 31 ++++++++ .../readme.md | 48 +++++++++++++ .../Solution.java | 33 +++++++++ .../readme.md | 40 +++++++++++ .../SolutionTest.java | 23 ++++++ .../SolutionTest.java | 23 ++++++ .../SolutionTest.java | 26 +++++++ .../SolutionTest.java | 18 +++++ .../SolutionTest.java | 18 +++++ .../SolutionTest.java | 18 +++++ .../SolutionTest.java | 24 +++++++ .../SolutionTest.java | 18 +++++ 24 files changed, 846 insertions(+) create mode 100644 src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java create mode 100644 src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/readme.md create mode 100644 src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java create mode 100644 src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/readme.md create mode 100644 src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java create mode 100644 src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/readme.md create mode 100644 src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java create mode 100644 src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/readme.md create mode 100644 src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java create mode 100644 src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/readme.md create mode 100644 src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java create mode 100644 src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/readme.md create mode 100644 src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java create mode 100644 src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/readme.md create mode 100644 src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java create mode 100644 src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/readme.md create mode 100644 src/test/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3724_minimum_operations_to_transform_array/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/SolutionTest.java diff --git a/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java new file mode 100644 index 000000000..caf14c752 --- /dev/null +++ b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java @@ -0,0 +1,70 @@ +package g3701_3800.s3722_lexicographically_smallest_string_after_reverse; + +// #Medium #Biweekly_Contest_168 #2025_10_26_Time_8_ms_(100.00%)_Space_44.98_MB_(100.00%) + +public class Solution { + public String lexSmallest(String s) { + int n = s.length(); + char[] arr = s.toCharArray(); + char[] best = arr.clone(); + // Check all reverse first k operations + for (int k = 1; k <= n; k++) { + if (isBetterReverseFirstK(arr, k, best)) { + updateBestReverseFirstK(arr, k, best); + } + } + // Check all reverse last k operations + for (int k = 1; k <= n; k++) { + if (isBetterReverseLastK(arr, k, best)) { + updateBestReverseLastK(arr, k, best); + } + } + return new String(best); + } + + private boolean isBetterReverseFirstK(char[] arr, int k, char[] best) { + for (int i = 0; i < arr.length; i++) { + char currentChar = (i < k) ? arr[k - 1 - i] : arr[i]; + if (currentChar < best[i]) { + return true; + } + if (currentChar > best[i]) { + return false; + } + } + return false; + } + + private boolean isBetterReverseLastK(char[] arr, int k, char[] best) { + int n = arr.length; + for (int i = 0; i < n; i++) { + char currentChar = (i < n - k) ? arr[i] : arr[n - 1 - (i - (n - k))]; + if (currentChar < best[i]) { + return true; + } + if (currentChar > best[i]) { + return false; + } + } + return false; + } + + private void updateBestReverseFirstK(char[] arr, int k, char[] best) { + for (int i = 0; i < k; i++) { + best[i] = arr[k - 1 - i]; + } + if (arr.length - k >= 0) { + System.arraycopy(arr, k, best, k, arr.length - k); + } + } + + private void updateBestReverseLastK(char[] arr, int k, char[] best) { + int n = arr.length; + if (n - k >= 0) { + System.arraycopy(arr, 0, best, 0, n - k); + } + for (int i = 0; i < k; i++) { + best[n - k + i] = arr[n - 1 - i]; + } + } +} diff --git a/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/readme.md b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/readme.md new file mode 100644 index 000000000..ca133b0b0 --- /dev/null +++ b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/readme.md @@ -0,0 +1,52 @@ +3722\. Lexicographically Smallest String After Reverse + +Medium + +You are given a string `s` of length `n` consisting of lowercase English letters. + +You must perform **exactly** one operation by choosing any integer `k` such that `1 <= k <= n` and either: + +* reverse the **first** `k` characters of `s`, or +* reverse the **last** `k` characters of `s`. + +Return the **lexicographically smallest** string that can be obtained after **exactly** one such operation. + +A string `a` is **lexicographically smaller** than a string `b` if, at the first position where they differ, `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`. If the first `min(a.length, b.length)` characters are the same, then the shorter string is considered lexicographically smaller. + +**Example 1:** + +**Input:** s = "dcab" + +**Output:** "acdb" + +**Explanation:** + +* Choose `k = 3`, reverse the first 3 characters. +* Reverse `"dca"` to `"acd"`, resulting string `s = "acdb"`, which is the lexicographically smallest string achievable. + +**Example 2:** + +**Input:** s = "abba" + +**Output:** "aabb" + +**Explanation:** + +* Choose `k = 3`, reverse the last 3 characters. +* Reverse `"bba"` to `"abb"`, so the resulting string is `"aabb"`, which is the lexicographically smallest string achievable. + +**Example 3:** + +**Input:** s = "zxy" + +**Output:** "xzy" + +**Explanation:** + +* Choose `k = 2`, reverse the first 2 characters. +* Reverse `"zx"` to `"xz"`, so the resulting string is `"xzy"`, which is the lexicographically smallest string achievable. + +**Constraints:** + +* `1 <= n == s.length <= 1000` +* `s` consists of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java new file mode 100644 index 000000000..99934812e --- /dev/null +++ b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java @@ -0,0 +1,27 @@ +package g3701_3800.s3723_maximize_sum_of_squares_of_digits; + +// #Medium #Biweekly_Contest_168 #2025_10_26_Time_8_ms_(99.81%)_Space_45.78_MB_(_%) + +public class Solution { + public String maxSumOfSquares(int places, int sum) { + String ans = ""; + int nines = sum / 9; + if (places < nines) { + return ans; + } else if (places == nines) { + int remSum = sum - nines * 9; + if (remSum > 0) { + return ans; + } + ans = "9".repeat(nines); + } else { + int remSum = sum - nines * 9; + ans = "9".repeat(nines) + remSum; + int extra = places - ans.length(); + if (extra > 0) { + ans = ans + ("0".repeat(extra)); + } + } + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/readme.md b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/readme.md new file mode 100644 index 000000000..8355d7c6b --- /dev/null +++ b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/readme.md @@ -0,0 +1,60 @@ +3723\. Maximize Sum of Squares of Digits + +Medium + +You are given two **positive** integers `num` and `sum`. + +A positive integer `n` is **good** if it satisfies both of the following: + +* The number of digits in `n` is **exactly** `num`. +* The sum of digits in `n` is **exactly** `sum`. + +The **score** of a **good** integer `n` is the sum of the squares of digits in `n`. + +Return a **string** denoting the **good** integer `n` that achieves the **maximum** **score**. If there are multiple possible integers, return the **maximum** one. If no such integer exists, return an empty string. + +**Example 1:** + +**Input:** num = 2, sum = 3 + +**Output:** "30" + +**Explanation:** + +There are 3 good integers: 12, 21, and 30. + +* The score of 12 is 12 + 22 = 5. +* The score of 21 is 22 + 12 = 5. +* The score of 30 is 32 + 02 = 9. + +The maximum score is 9, which is achieved by the good integer 30. Therefore, the answer is `"30"`. + +**Example 2:** + +**Input:** num = 2, sum = 17 + +**Output:** "98" + +**Explanation:** + +There are 2 good integers: 89 and 98. + +* The score of 89 is 82 + 92 = 145. +* The score of 98 is 92 + 82 = 145. + +The maximum score is 145. The maximum good integer that achieves this score is 98. Therefore, the answer is `"98"`. + +**Example 3:** + +**Input:** num = 1, sum = 10 + +**Output:** "" + +**Explanation:** + +There are no integers that have exactly 1 digit and whose digits sum to 10. Therefore, the answer is `""`. + +**Constraints:** + +* 1 <= num <= 2 * 105 +* 1 <= sum <= 2 * 106 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java new file mode 100644 index 000000000..73aa8b90a --- /dev/null +++ b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java @@ -0,0 +1,28 @@ +package g3701_3800.s3724_minimum_operations_to_transform_array; + +// #Medium #Biweekly_Contest_168 #2025_10_26_Time_2_ms_(100.00%)_Space_61.61_MB_(100.00%) + +public class Solution { + public long minOperations(int[] nums1, int[] nums2) { + int n = nums1.length; + int last = nums2[n]; + long steps = 1; + long minDiffFromLast = Long.MAX_VALUE; + for (int i = 0; i < n; i++) { + int min = Math.min(nums1[i], nums2[i]); + int max = Math.max(nums1[i], nums2[i]); + steps += Math.abs(max - min); + if (minDiffFromLast > 0) { + if (min <= last && last <= max) { + minDiffFromLast = 0; + } else { + minDiffFromLast = + Math.min( + minDiffFromLast, + Math.min(Math.abs(min - last), Math.abs(max - last))); + } + } + } + return steps + minDiffFromLast; + } +} diff --git a/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/readme.md b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/readme.md new file mode 100644 index 000000000..3aa5f51bd --- /dev/null +++ b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/readme.md @@ -0,0 +1,71 @@ +3724\. Minimum Operations to Transform Array + +Medium + +You are given two integer arrays `nums1` of length `n` and `nums2` of length `n + 1`. + +You want to transform `nums1` into `nums2` using the **minimum** number of operations. + +You may perform the following operations **any** number of times, each time choosing an index `i`: + +* **Increase** `nums1[i]` by 1. +* **Decrease** `nums1[i]` by 1. +* **Append** `nums1[i]` to the **end** of the array. + +Return the **minimum** number of operations required to transform `nums1` into `nums2`. + +**Example 1:** + +**Input:** nums1 = [2,8], nums2 = [1,7,3] + +**Output:** 4 + +**Explanation:** + +| Step | `i` | Operation | `nums1[i]` | Updated `nums1` | +|------|------|------------|-------------|----------------| +| 1 | 0 | Append | - | [2, 8, 2] | +| 2 | 0 | Decrement | Decreases to 1 | [1, 8, 2] | +| 3 | 1 | Decrement | Decreases to 7 | [1, 7, 2] | +| 4 | 2 | Increment | Increases to 3 | [1, 7, 3] | + +Thus, after 4 operations `nums1` is transformed into `nums2`. + +**Example 2:** + +**Input:** nums1 = [1,3,6], nums2 = [2,4,5,3] + +**Output:** 4 + +**Explanation:** + +| Step | `i` | Operation | `nums1[i]` | Updated `nums1` | +|------|------|------------|-------------|----------------| +| 1 | 1 | Append | - | [1, 3, 6, 3] | +| 2 | 0 | Increment | Increases to 2 | [2, 3, 6, 3] | +| 3 | 1 | Increment | Increases to 4 | [2, 4, 6, 3] | +| 4 | 2 | Decrement | Decreases to 5 | [2, 4, 5, 3] | + +Thus, after 4 operations `nums1` is transformed into `nums2`. + +**Example 3:** + +**Input:** nums1 = [2], nums2 = [3,4] + +**Output:** 3 + +**Explanation:** + +| Step | `i` | Operation | `nums1[i]` | Updated `nums1` | +|------|------|------------|-------------|----------------| +| 1 | 0 | Increment | Increases to 3 | [3] | +| 2 | 0 | Append | - | [3, 3] | +| 3 | 1 | Increment | Increases to 4 | [3, 4] | + +Thus, after 3 operations `nums1` is transformed into `nums2`. + +**Constraints:** + +* 1 <= n == nums1.length <= 105 +* `nums2.length == n + 1` +* 1 <= nums1[i], nums2[i] <= 105 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java new file mode 100644 index 000000000..5245fcfe7 --- /dev/null +++ b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java @@ -0,0 +1,51 @@ +package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows; + +// #Hard #Biweekly_Contest_168 #2025_10_26_Time_31_ms_(92.56%)_Space_49.21_MB_(_%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + private static final int MOD = 1_000_000_007; + + public int countCoprime(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + int maxVal = 0; + for (int[] ints : mat) { + for (int j = 0; j < n; j++) { + maxVal = Math.max(maxVal, ints[j]); + } + } + Map gcdWays = new HashMap<>(); + for (int g = maxVal; g >= 1; g--) { + long ways = countWaysWithDivisor(mat, g, m, n); + if (ways > 0) { + for (int multiple = 2 * g; multiple <= maxVal; multiple += g) { + if (gcdWays.containsKey(multiple)) { + ways = (ways - gcdWays.get(multiple) + MOD) % MOD; + } + } + gcdWays.put(g, ways); + } + } + return gcdWays.getOrDefault(1, 0L).intValue(); + } + + private long countWaysWithDivisor(int[][] matrix, int divisor, int rows, int cols) { + long totalWays = 1; + for (int row = 0; row < rows; row++) { + int validChoices = 0; + for (int col = 0; col < cols; col++) { + if (matrix[row][col] % divisor == 0) { + validChoices++; + } + } + if (validChoices == 0) { + return 0; + } + totalWays = (totalWays * validChoices) % MOD; + } + return totalWays; + } +} diff --git a/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/readme.md b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/readme.md new file mode 100644 index 000000000..a56bd7eac --- /dev/null +++ b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/readme.md @@ -0,0 +1,42 @@ +3725\. Count Ways to Choose Coprime Integers from Rows + +Hard + +You are given a `m x n` matrix `mat` of positive integers. + +Return an integer denoting the number of ways to choose **exactly one** integer from each row of `mat` such that the **greatest common divisor** of all chosen integers is 1. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** mat = [[1,2],[3,4]] + +**Output:** 3 + +**Explanation:** + +| Chosen integer in the first row | Chosen integer in the second row | Greatest common divisor of chosen integers | +|---------------------------------|----------------------------------|--------------------------------------------| +| 1 | 3 | 1 | +| 1 | 4 | 1 | +| 2 | 3 | 1 | +| 2 | 4 | 2 | + +3 of these combinations have a greatest common divisor of 1. Therefore, the answer is 3. + +**Example 2:** + +**Input:** mat = [[2,2],[2,2]] + +**Output:** 0 + +**Explanation:** + +Every combination has a greatest common divisor of 2. Therefore, the answer is 0. + +**Constraints:** + +* `1 <= m == mat.length <= 150` +* `1 <= n == mat[i].length <= 150` +* `1 <= mat[i][j] <= 150` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java new file mode 100644 index 000000000..0b53c326f --- /dev/null +++ b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java @@ -0,0 +1,16 @@ +package g3701_3800.s3726_remove_zeros_in_decimal_representation; + +// #Easy #Weekly_Contest_473 #2025_10_26_Time_9_ms_(_%)_Space_41.93_MB_(_%) + +public class Solution { + public long removeZeros(long n) { + StringBuilder x = new StringBuilder(); + String s = Long.toString(n); + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) != '0') { + x.append(s.charAt(i)); + } + } + return Long.parseLong(x.toString()); + } +} diff --git a/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/readme.md b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/readme.md new file mode 100644 index 000000000..96e7fdc7a --- /dev/null +++ b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/readme.md @@ -0,0 +1,31 @@ +3726\. Remove Zeros in Decimal Representation + +Easy + +You are given a **positive** integer `n`. + +Return the integer obtained by removing all zeros from the decimal representation of `n`. + +**Example 1:** + +**Input:** n = 1020030 + +**Output:** 123 + +**Explanation:** + +After removing all zeros from 1**0**2**00**3**0**, we get 123. + +**Example 2:** + +**Input:** n = 1 + +**Output:** 1 + +**Explanation:** + +1 has no zero in its decimal representation. Therefore, the answer is 1. + +**Constraints:** + +* 1 <= n <= 1015 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java new file mode 100644 index 000000000..a7c4be9e2 --- /dev/null +++ b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java @@ -0,0 +1,34 @@ +package g3701_3800.s3727_maximum_alternating_sum_of_squares; + +// #Medium #Weekly_Contest_473 #2025_10_26_Time_46_ms_(100.00%)_Space_62.34_MB_(_%) + +public class Solution { + public long maxAlternatingSum(int[] nums) { + int n = nums.length; + long sum = 0; + int i = 0; + int j = n - 1; + for (int k = 0; k < n; k++) { + nums[k] = nums[k] * nums[k]; + } + java.util.Arrays.sort(nums); + java.util.List ans = new java.util.ArrayList<>(); + while (i < j) { + ans.add((long) nums[j]); + ans.add((long) nums[i]); + j--; + i++; + } + if ((n & 1) == 1) { + ans.add((long) nums[n / 2]); + } + for (int k = 0; k < n; k++) { + if (k % 2 == 0) { + sum += ans.get(k); + } else { + sum -= ans.get(k); + } + } + return sum; + } +} diff --git a/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/readme.md b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/readme.md new file mode 100644 index 000000000..7dc315730 --- /dev/null +++ b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/readme.md @@ -0,0 +1,44 @@ +3727\. Maximum Alternating Sum of Squares + +Medium + +You are given an integer array `nums`. You may **rearrange the elements** in any order. + +The **alternating score** of an array `arr` is defined as: + +* score = arr[0]2 - arr[1]2 + arr[2]2 - arr[3]2 + ... + +Return an integer denoting the **maximum possible alternating score** of `nums` after rearranging its elements. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** 12 + +**Explanation:** + +A possible rearrangement for `nums` is `[2,1,3]`, which gives the maximum alternating score among all possible rearrangements. + +The alternating score is calculated as: + +score = 22 - 12 + 32 = 4 - 1 + 9 = 12 + +**Example 2:** + +**Input:** nums = [1,-1,2,-2,3,-3] + +**Output:** 16 + +**Explanation:** + +A possible rearrangement for `nums` is `[-3,-1,-2,1,3,2]`, which gives the maximum alternating score among all possible rearrangements. + +The alternating score is calculated as: + +score = (-3)2 - (-1)2 + (-2)2 - (1)2 + (3)2 - (2)2 = 9 - 1 + 4 - 1 + 9 - 4 = 16 + +**Constraints:** + +* 1 <= nums.length <= 105 +* -4 * 104 <= nums[i] <= 4 * 104 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java new file mode 100644 index 000000000..ad26e9c4d --- /dev/null +++ b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java @@ -0,0 +1,31 @@ +package g3701_3800.s3728_stable_subarrays_with_equal_boundary_and_interior_sum; + +// #Medium #Weekly_Contest_473 #2025_10_26_Time_124_ms_(100.00%)_Space_62.66_MB_(100.00%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public long countStableSubarrays(int[] capacity) { + long n = capacity.length; + long res = 0; + long pre = 0; + Map> mpp = new HashMap<>(); + for (int i = 0; i < n; i++) { + if (mpp.containsKey((long) capacity[i])) { + Map t = mpp.get((long) capacity[i]); + Long cnt = t.get(pre - capacity[i]); + if (cnt != null) { + res += cnt; + } + } + pre += capacity[i]; + Map t = mpp.computeIfAbsent((long) capacity[i], k -> new HashMap<>()); + t.put(pre, t.getOrDefault(pre, 0L) + 1L); + if (i > 0 && capacity[i] == 0 && capacity[i - 1] == 0) { + res--; + } + } + return res; + } +} diff --git a/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/readme.md b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/readme.md new file mode 100644 index 000000000..da434b7fb --- /dev/null +++ b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/readme.md @@ -0,0 +1,48 @@ +3728\. Stable Subarrays With Equal Boundary and Interior Sum + +Medium + +You are given an integer array `capacity`. + +A **non-empty subarrays** `capacity[l..r]` is considered **stable** if: + +* Its length is **at least** 3. +* The **first** and **last** elements are each equal to the **sum** of all elements **strictly between** them (i.e., `capacity[l] = capacity[r] = capacity[l + 1] + capacity[l + 2] + ... + capacity[r - 1]`). + +Return an integer denoting the number of **stable subarrays**. + +**Example 1:** + +**Input:** capacity = [9,3,3,3,9] + +**Output:** 2 + +**Explanation:** + +* `[9,3,3,3,9]` is stable because the first and last elements are both 9, and the sum of the elements strictly between them is `3 + 3 + 3 = 9`. +* `[3,3,3]` is stable because the first and last elements are both 3, and the sum of the elements strictly between them is 3. + +**Example 2:** + +**Input:** capacity = [1,2,3,4,5] + +**Output:** 0 + +**Explanation:** + +No subarray of length at least 3 has equal first and last elements, so the answer is 0. + +**Example 3:** + +**Input:** capacity = [-4,4,0,0,-8,-4] + +**Output:** 1 + +**Explanation:** + +`[-4,4,0,0,-8,-4]` is stable because the first and last elements are both -4, and the sum of the elements strictly between them is `4 + 0 + 0 + (-8) = -4` + +**Constraints:** + +* 3 <= capacity.length <= 105 +* -109 <= capacity[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java new file mode 100644 index 000000000..95a344bdb --- /dev/null +++ b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java @@ -0,0 +1,33 @@ +package g3701_3800.s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array; + +// #Hard #Weekly_Contest_473 #2025_10_26_Time_67_ms_(50.00%)_Space_61.73_MB_(50.00%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public long numGoodSubarrays(int[] nums, int k) { + Map cnt = new HashMap<>(); + cnt.put(0, 1L); + int pre = 0; + int n = nums.length; + long res = 0; + for (int a : nums) { + pre = (pre + a) % k; + res += cnt.getOrDefault(pre, 0L); + cnt.put(pre, cnt.getOrDefault(pre, 0L) + 1L); + } + for (int i = 0; i < n; ) { + int j = i; + while (j < n && nums[j] == nums[i]) ++j; + int l = j - i; + for (int ll = 1; ll < l; ++ll) { + if ((long) ll * nums[i] % k == 0) { + res -= (l - ll); + } + } + i = j; + } + return res; + } +} diff --git a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/readme.md b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/readme.md new file mode 100644 index 000000000..00d7143a4 --- /dev/null +++ b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/readme.md @@ -0,0 +1,40 @@ +3729\. Count Distinct Subarrays Divisible by K in Sorted Array + +Hard + +You are given an integer array `nums` **sorted** in **non-descending** order and a positive integer `k`. + +A ****non-empty subarrays**** of `nums` is **good** if the sum of its elements is **divisible** by `k`. + +Return an integer denoting the number of **distinct** **good** subarrays of `nums`. + +Subarrays are **distinct** if their sequences of values are. For example, there are 3 **distinct** subarrays in `[1, 1, 1]`, namely `[1]`, `[1, 1]`, and `[1, 1, 1]`. + +**Example 1:** + +**Input:** nums = [1,2,3], k = 3 + +**Output:** 3 + +**Explanation:** + +The good subarrays are `[1, 2]`, `[3]`, and `[1, 2, 3]`. For example, `[1, 2, 3]` is good because the sum of its elements is `1 + 2 + 3 = 6`, and `6 % k = 6 % 3 = 0`. + +**Example 2:** + +**Input:** nums = [2,2,2,2,2,2], k = 6 + +**Output:** 2 + +**Explanation:** + +The good subarrays are `[2, 2, 2]` and `[2, 2, 2, 2, 2, 2]`. For example, `[2, 2, 2]` is good because the sum of its elements is `2 + 2 + 2 = 6`, and `6 % k = 6 % 6 = 0`. + +Note that `[2, 2, 2]` is counted only once. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* `nums` is sorted in non-descending order. +* 1 <= k <= 109 \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/SolutionTest.java b/src/test/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/SolutionTest.java new file mode 100644 index 000000000..eb8cf7413 --- /dev/null +++ b/src/test/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3722_lexicographically_smallest_string_after_reverse; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void lexSmallest() { + assertThat(new Solution().lexSmallest("dcab"), equalTo("acdb")); + } + + @Test + void lexSmallest2() { + assertThat(new Solution().lexSmallest("abba"), equalTo("aabb")); + } + + @Test + void lexSmallest3() { + assertThat(new Solution().lexSmallest("zxy"), equalTo("xzy")); + } +} diff --git a/src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java b/src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java new file mode 100644 index 000000000..ff985b2a8 --- /dev/null +++ b/src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3723_maximize_sum_of_squares_of_digits; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxSumOfSquares() { + assertThat(new Solution().maxSumOfSquares(2, 3), equalTo("30")); + } + + @Test + void maxSumOfSquares2() { + assertThat(new Solution().maxSumOfSquares(2, 17), equalTo("98")); + } + + @Test + void maxSumOfSquares3() { + assertThat(new Solution().maxSumOfSquares(1, 10), equalTo("")); + } +} diff --git a/src/test/java/g3701_3800/s3724_minimum_operations_to_transform_array/SolutionTest.java b/src/test/java/g3701_3800/s3724_minimum_operations_to_transform_array/SolutionTest.java new file mode 100644 index 000000000..87e6f9642 --- /dev/null +++ b/src/test/java/g3701_3800/s3724_minimum_operations_to_transform_array/SolutionTest.java @@ -0,0 +1,26 @@ +package g3701_3800.s3724_minimum_operations_to_transform_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minOperations() { + assertThat( + new Solution().minOperations(new int[] {2, 8}, new int[] {1, 7, 3}), equalTo(4L)); + } + + @Test + void minOperations2() { + assertThat( + new Solution().minOperations(new int[] {1, 3, 6}, new int[] {2, 4, 5, 3}), + equalTo(4L)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(new int[] {2}, new int[] {3, 4}), equalTo(3L)); + } +} diff --git a/src/test/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/SolutionTest.java b/src/test/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/SolutionTest.java new file mode 100644 index 000000000..fc86a7d2d --- /dev/null +++ b/src/test/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/SolutionTest.java @@ -0,0 +1,18 @@ +package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countCoprime() { + assertThat(new Solution().countCoprime(new int[][] {{1, 2}, {3, 4}}), equalTo(3)); + } + + @Test + void countCoprime2() { + assertThat(new Solution().countCoprime(new int[][] {{2, 2}, {2, 2}}), equalTo(0)); + } +} diff --git a/src/test/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/SolutionTest.java b/src/test/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/SolutionTest.java new file mode 100644 index 000000000..d8243393f --- /dev/null +++ b/src/test/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/SolutionTest.java @@ -0,0 +1,18 @@ +package g3701_3800.s3726_remove_zeros_in_decimal_representation; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void removeZeros() { + assertThat(new Solution().removeZeros(1020030L), equalTo(123L)); + } + + @Test + void removeZeros2() { + assertThat(new Solution().removeZeros(1L), equalTo(1L)); + } +} diff --git a/src/test/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/SolutionTest.java b/src/test/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/SolutionTest.java new file mode 100644 index 000000000..305fd8c26 --- /dev/null +++ b/src/test/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/SolutionTest.java @@ -0,0 +1,18 @@ +package g3701_3800.s3727_maximum_alternating_sum_of_squares; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxAlternatingSum() { + assertThat(new Solution().maxAlternatingSum(new int[] {1, 2, 3}), equalTo(12L)); + } + + @Test + void maxAlternatingSum2() { + assertThat(new Solution().maxAlternatingSum(new int[] {1, -1, 2, -2, 3, -3}), equalTo(16L)); + } +} diff --git a/src/test/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/SolutionTest.java b/src/test/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/SolutionTest.java new file mode 100644 index 000000000..3c89b9f04 --- /dev/null +++ b/src/test/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/SolutionTest.java @@ -0,0 +1,24 @@ +package g3701_3800.s3728_stable_subarrays_with_equal_boundary_and_interior_sum; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countStableSubarrays() { + assertThat(new Solution().countStableSubarrays(new int[] {9, 3, 3, 3, 9}), equalTo(2L)); + } + + @Test + void countStableSubarrays2() { + assertThat(new Solution().countStableSubarrays(new int[] {1, 2, 3, 4, 5}), equalTo(0L)); + } + + @Test + void countStableSubarrays3() { + assertThat( + new Solution().countStableSubarrays(new int[] {-4, 4, 0, 0, -8, -4}), equalTo(1L)); + } +} diff --git a/src/test/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/SolutionTest.java b/src/test/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/SolutionTest.java new file mode 100644 index 000000000..a95428013 --- /dev/null +++ b/src/test/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/SolutionTest.java @@ -0,0 +1,18 @@ +package g3701_3800.s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numGoodSubarrays() { + assertThat(new Solution().numGoodSubarrays(new int[] {1, 2, 3}, 3), equalTo(3L)); + } + + @Test + void numGoodSubarrays2() { + assertThat(new Solution().numGoodSubarrays(new int[] {2, 2, 2, 2, 2, 2}, 6), equalTo(2L)); + } +} From 0c79474ddd872d067df4500d8b24f5c46d3f33c4 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Oct 2025 18:37:50 +0200 Subject: [PATCH 2/6] Fixed style --- .../Solution.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java index 95a344bdb..b8674f0f4 100644 --- a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java +++ b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java @@ -19,7 +19,9 @@ public long numGoodSubarrays(int[] nums, int k) { } for (int i = 0; i < n; ) { int j = i; - while (j < n && nums[j] == nums[i]) ++j; + while (j < n && nums[j] == nums[i]) { + ++j; + } int l = j - i; for (int ll = 1; ll < l; ++ll) { if ((long) ll * nums[i] % k == 0) { From 0aaeb81e7914ce05f880d65cb1afe61ed8fa6afe Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Oct 2025 18:40:07 +0200 Subject: [PATCH 3/6] Fixed sonar --- .../Solution.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java index b8674f0f4..976df2b3d 100644 --- a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java +++ b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java @@ -17,7 +17,8 @@ public long numGoodSubarrays(int[] nums, int k) { res += cnt.getOrDefault(pre, 0L); cnt.put(pre, cnt.getOrDefault(pre, 0L) + 1L); } - for (int i = 0; i < n; ) { + int i = 0; + while (i < n) { int j = i; while (j < n && nums[j] == nums[i]) { ++j; From 9c709b226d6e358402b162bc71b8ecfe13a90a9c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Oct 2025 18:57:56 +0200 Subject: [PATCH 4/6] Added tests --- .../SolutionTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java b/src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java index ff985b2a8..0cf0d511c 100644 --- a/src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java +++ b/src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java @@ -20,4 +20,52 @@ void maxSumOfSquares2() { void maxSumOfSquares3() { assertThat(new Solution().maxSumOfSquares(1, 10), equalTo("")); } + + @Test + void maxSumOfSquares4() { + // sum = 27 → nines = 3 + // places = 2 < 3 → should return "" + String result = new Solution().maxSumOfSquares(2, 27); + assertThat(result, equalTo("")); + } + + @Test + void maxSumOfSquares5() { + // sum = 28 → nines = 3, remSum = 1 + // places = 3 == nines and remSum > 0 → should return "" + String result = new Solution().maxSumOfSquares(3, 28); + assertThat(result, equalTo("")); + } + + @Test + void maxSumOfSquares6() { + // sum = 27 → nines = 3, remSum = 0 + // places = 3 == nines and remSum == 0 → should return "999" + String result = new Solution().maxSumOfSquares(3, 27); + assertThat(result, equalTo("999")); + } + + @Test + void maxSumOfSquares7() { + // sum = 10 → nines = 1, remSum = 1 + // ans = "9" + "1" = "91", length = 2, places = 2 → no padding + String result = new Solution().maxSumOfSquares(2, 10); + assertThat(result, equalTo("91")); + } + + @Test + void maxSumOfSquares8() { + // sum = 10 → nines = 1, remSum = 1 + // ans = "9" + "1" = "91", length = 2, places = 4 → add two zeros + String result = new Solution().maxSumOfSquares(4, 10); + assertThat(result, equalTo("9100")); + } + + @Test + void maxSumOfSquares9() { + // sum = 5 → nines = 0, remSum = 5 → ans = "5" + // places = 3 → add two zeros + String result = new Solution().maxSumOfSquares(3, 5); + assertThat(result, equalTo("500")); + } } From cc09123283fb5e2a143152e3c58ec7ff3c3de511 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 28 Oct 2025 09:19:18 +0200 Subject: [PATCH 5/6] Updated tags --- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 39 ++++++++----------- .../Solution.java | 2 +- .../Solution.java | 2 +- 8 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java index caf14c752..34df30fbd 100644 --- a/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java +++ b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java @@ -1,6 +1,6 @@ package g3701_3800.s3722_lexicographically_smallest_string_after_reverse; -// #Medium #Biweekly_Contest_168 #2025_10_26_Time_8_ms_(100.00%)_Space_44.98_MB_(100.00%) +// #Medium #Biweekly_Contest_168 #2025_10_28_Time_8_ms_(100.00%)_Space_44.85_MB_(99.91%) public class Solution { public String lexSmallest(String s) { diff --git a/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java index 99934812e..acc7ef2c7 100644 --- a/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java +++ b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java @@ -1,6 +1,6 @@ package g3701_3800.s3723_maximize_sum_of_squares_of_digits; -// #Medium #Biweekly_Contest_168 #2025_10_26_Time_8_ms_(99.81%)_Space_45.78_MB_(_%) +// #Medium #Biweekly_Contest_168 #2025_10_28_Time_14_ms_(99.30%)_Space_46.14_MB_(70.05%) public class Solution { public String maxSumOfSquares(int places, int sum) { diff --git a/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java index 73aa8b90a..a64117a55 100644 --- a/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java +++ b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java @@ -1,6 +1,6 @@ package g3701_3800.s3724_minimum_operations_to_transform_array; -// #Medium #Biweekly_Contest_168 #2025_10_26_Time_2_ms_(100.00%)_Space_61.61_MB_(100.00%) +// #Medium #Biweekly_Contest_168 #2025_10_28_Time_2_ms_(100.00%)_Space_62.61_MB_(5.39%) public class Solution { public long minOperations(int[] nums1, int[] nums2) { diff --git a/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java index 5245fcfe7..f9e5a7d06 100644 --- a/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java +++ b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java @@ -1,6 +1,6 @@ package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows; -// #Hard #Biweekly_Contest_168 #2025_10_26_Time_31_ms_(92.56%)_Space_49.21_MB_(_%) +// #Hard #Biweekly_Contest_168 #2025_10_28_Time_30_ms_(93.77%)_Space_47.15_MB_(68.37%) import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java index 0b53c326f..8e336e7ca 100644 --- a/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java +++ b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java @@ -1,6 +1,6 @@ package g3701_3800.s3726_remove_zeros_in_decimal_representation; -// #Easy #Weekly_Contest_473 #2025_10_26_Time_9_ms_(_%)_Space_41.93_MB_(_%) +// #Easy #Weekly_Contest_473 #2025_10_28_Time_1_ms_(98.76%)_Space_40.81_MB_(93.77%) public class Solution { public long removeZeros(long n) { diff --git a/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java index a7c4be9e2..b6f3d6b2b 100644 --- a/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java +++ b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java @@ -1,34 +1,27 @@ package g3701_3800.s3727_maximum_alternating_sum_of_squares; -// #Medium #Weekly_Contest_473 #2025_10_26_Time_46_ms_(100.00%)_Space_62.34_MB_(_%) +// #Medium #Weekly_Contest_473 #2025_10_28_Time_10_ms_(99.97%)_Space_63.08_MB_(17.57%) public class Solution { public long maxAlternatingSum(int[] nums) { int n = nums.length; - long sum = 0; - int i = 0; - int j = n - 1; - for (int k = 0; k < n; k++) { - nums[k] = nums[k] * nums[k]; + int need = n / 2; + int maxa = 40000; + int[] freq = new int[maxa + 1]; + long total = 0; + for (int x : nums) { + int a = Math.abs(x); + freq[a]++; + total += 1L * a * a; } - java.util.Arrays.sort(nums); - java.util.List ans = new java.util.ArrayList<>(); - while (i < j) { - ans.add((long) nums[j]); - ans.add((long) nums[i]); - j--; - i++; - } - if ((n & 1) == 1) { - ans.add((long) nums[n / 2]); - } - for (int k = 0; k < n; k++) { - if (k % 2 == 0) { - sum += ans.get(k); - } else { - sum -= ans.get(k); + long small = 0; + for (int a = 0; a <= maxa && need > 0; a++) { + int take = Math.min(freq[a], need); + if (take > 0) { + small += 1L * a * a * take; + need -= take; } } - return sum; + return total - 2 * small; } } diff --git a/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java index ad26e9c4d..0831d8b23 100644 --- a/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java +++ b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java @@ -1,6 +1,6 @@ package g3701_3800.s3728_stable_subarrays_with_equal_boundary_and_interior_sum; -// #Medium #Weekly_Contest_473 #2025_10_26_Time_124_ms_(100.00%)_Space_62.66_MB_(100.00%) +// #Medium #Weekly_Contest_473 #2025_10_28_Time_96_ms_(68.14%)_Space_62.66_MB_(83.54%) import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java index 976df2b3d..2d342dfda 100644 --- a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java +++ b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java @@ -1,6 +1,6 @@ package g3701_3800.s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array; -// #Hard #Weekly_Contest_473 #2025_10_26_Time_67_ms_(50.00%)_Space_61.73_MB_(50.00%) +// #Hard #Weekly_Contest_473 #2025_10_28_Time_61_ms_(88.70%)_Space_60.47_MB_(80.43%) import java.util.HashMap; import java.util.Map; From 10b39e1ae5224f7e0509a5ead4c5090cb9e7c573 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Oct 2025 08:13:33 +0200 Subject: [PATCH 6/6] Updated tags --- .../Solution.java | 3 +- .../Solution.java | 3 +- .../Solution.java | 3 +- .../Solution.java | 3 +- .../Solution.java | 3 +- .../Solution.java | 3 +- .../Solution.java | 39 ++++++++++-------- .../Solution.java | 41 ++++++++----------- 8 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java index 34df30fbd..e9addecd2 100644 --- a/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java +++ b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java @@ -1,6 +1,7 @@ package g3701_3800.s3722_lexicographically_smallest_string_after_reverse; -// #Medium #Biweekly_Contest_168 #2025_10_28_Time_8_ms_(100.00%)_Space_44.85_MB_(99.91%) +// #Medium #Binary_Search #Two_Pointers #Enumeration #Biweekly_Contest_168 +// #2025_10_29_Time_7_ms_(100.00%)_Space_45.70_MB_(100.00%) public class Solution { public String lexSmallest(String s) { diff --git a/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java index acc7ef2c7..b6d9be665 100644 --- a/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java +++ b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java @@ -1,6 +1,7 @@ package g3701_3800.s3723_maximize_sum_of_squares_of_digits; -// #Medium #Biweekly_Contest_168 #2025_10_28_Time_14_ms_(99.30%)_Space_46.14_MB_(70.05%) +// #Medium #Math #Greedy #Biweekly_Contest_168 +// #2025_10_29_Time_14_ms_(98.69%)_Space_46.36_MB_(47.20%) public class Solution { public String maxSumOfSquares(int places, int sum) { diff --git a/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java index a64117a55..766b1f73e 100644 --- a/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java +++ b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java @@ -1,6 +1,7 @@ package g3701_3800.s3724_minimum_operations_to_transform_array; -// #Medium #Biweekly_Contest_168 #2025_10_28_Time_2_ms_(100.00%)_Space_62.61_MB_(5.39%) +// #Medium #Array #Greedy #Biweekly_Contest_168 +// #2025_10_29_Time_2_ms_(100.00%)_Space_61.71_MB_(16.98%) public class Solution { public long minOperations(int[] nums1, int[] nums2) { diff --git a/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java index f9e5a7d06..23c85b152 100644 --- a/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java +++ b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java @@ -1,6 +1,7 @@ package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows; -// #Hard #Biweekly_Contest_168 #2025_10_28_Time_30_ms_(93.77%)_Space_47.15_MB_(68.37%) +// #Hard #Array #Dynamic_Programming #Math #Matrix #Number_Theory #Combinatorics +// #Biweekly_Contest_168 #2025_10_29_Time_31_ms_(94.07%)_Space_47.74_MB_(49.21%) import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java index 8e336e7ca..3855d5deb 100644 --- a/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java +++ b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java @@ -1,6 +1,7 @@ package g3701_3800.s3726_remove_zeros_in_decimal_representation; -// #Easy #Weekly_Contest_473 #2025_10_28_Time_1_ms_(98.76%)_Space_40.81_MB_(93.77%) +// #Easy #Math #Simulation #Weekly_Contest_473 +// #2025_10_29_Time_1_ms_(98.59%)_Space_40.46_MB_(99.88%) public class Solution { public long removeZeros(long n) { diff --git a/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java index b6f3d6b2b..83e32aaf8 100644 --- a/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java +++ b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java @@ -1,6 +1,7 @@ package g3701_3800.s3727_maximum_alternating_sum_of_squares; -// #Medium #Weekly_Contest_473 #2025_10_28_Time_10_ms_(99.97%)_Space_63.08_MB_(17.57%) +// #Medium #Array #Sorting #Greedy #Weekly_Contest_473 +// #2025_10_29_Time_9_ms_(100.00%)_Space_62.45_MB_(32.41%) public class Solution { public long maxAlternatingSum(int[] nums) { diff --git a/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java index 0831d8b23..e9cf1f3c7 100644 --- a/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java +++ b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java @@ -1,31 +1,34 @@ package g3701_3800.s3728_stable_subarrays_with_equal_boundary_and_interior_sum; -// #Medium #Weekly_Contest_473 #2025_10_28_Time_96_ms_(68.14%)_Space_62.66_MB_(83.54%) +// #Medium #Array #Hash_Table #Prefix_Sum #Weekly_Contest_473 +// #2025_10_29_Time_71_ms_(98.21%)_Space_61.51_MB_(88.86%) import java.util.HashMap; import java.util.Map; public class Solution { public long countStableSubarrays(int[] capacity) { - long n = capacity.length; - long res = 0; - long pre = 0; - Map> mpp = new HashMap<>(); - for (int i = 0; i < n; i++) { - if (mpp.containsKey((long) capacity[i])) { - Map t = mpp.get((long) capacity[i]); - Long cnt = t.get(pre - capacity[i]); - if (cnt != null) { - res += cnt; + long sum = 0; + Map> map = new HashMap<>(); + int index = 0; + long ans = 0; + for (int c : capacity) { + sum += c; + Map elementMap = map.get(c); + if (elementMap == null) { + elementMap = new HashMap<>(); + map.put(c, elementMap); + elementMap.put(sum, 1); + } else { + Integer orDefault = elementMap.getOrDefault(sum - 2 * c, 0); + elementMap.put(sum, elementMap.getOrDefault(sum, 0) + 1); + if (c == 0 && capacity[index - 1] == 0) { + orDefault--; } + ans += orDefault; } - pre += capacity[i]; - Map t = mpp.computeIfAbsent((long) capacity[i], k -> new HashMap<>()); - t.put(pre, t.getOrDefault(pre, 0L) + 1L); - if (i > 0 && capacity[i] == 0 && capacity[i - 1] == 0) { - res--; - } + index++; } - return res; + return ans; } } diff --git a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java index 2d342dfda..06650ee14 100644 --- a/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java +++ b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java @@ -1,36 +1,31 @@ package g3701_3800.s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array; -// #Hard #Weekly_Contest_473 #2025_10_28_Time_61_ms_(88.70%)_Space_60.47_MB_(80.43%) +// #Hard #Array #Hash_Table #Prefix_Sum #Weekly_Contest_473 +// #2025_10_29_Time_33_ms_(99.78%)_Space_62.35_MB_(50.99%) import java.util.HashMap; import java.util.Map; public class Solution { public long numGoodSubarrays(int[] nums, int k) { - Map cnt = new HashMap<>(); - cnt.put(0, 1L); - int pre = 0; - int n = nums.length; - long res = 0; - for (int a : nums) { - pre = (pre + a) % k; - res += cnt.getOrDefault(pre, 0L); - cnt.put(pre, cnt.getOrDefault(pre, 0L) + 1L); - } - int i = 0; - while (i < n) { - int j = i; - while (j < n && nums[j] == nums[i]) { - ++j; - } - int l = j - i; - for (int ll = 1; ll < l; ++ll) { - if ((long) ll * nums[i] % k == 0) { - res -= (l - ll); + Map cnt = new HashMap<>(nums.length, 1); + cnt.put(0, 1); + long sum = 0; + int lastStart = 0; + long ans = 0; + for (int i = 0; i < nums.length; i++) { + int x = nums[i]; + if (i > 0 && x != nums[i - 1]) { + long s = sum; + for (int t = i - lastStart; t > 0; t--) { + cnt.merge((int) (s % k), 1, Integer::sum); + s -= nums[i - 1]; } + lastStart = i; } - i = j; + sum += x; + ans += cnt.getOrDefault((int) (sum % k), 0); } - return res; + return ans; } }