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..e9addecd2 --- /dev/null +++ b/src/main/java/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.java @@ -0,0 +1,71 @@ +package g3701_3800.s3722_lexicographically_smallest_string_after_reverse; + +// #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) { + 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..b6d9be665 --- /dev/null +++ b/src/main/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.java @@ -0,0 +1,28 @@ +package g3701_3800.s3723_maximize_sum_of_squares_of_digits; + +// #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) { + 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..766b1f73e --- /dev/null +++ b/src/main/java/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.java @@ -0,0 +1,29 @@ +package g3701_3800.s3724_minimum_operations_to_transform_array; + +// #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) { + 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..23c85b152 --- /dev/null +++ b/src/main/java/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.java @@ -0,0 +1,52 @@ +package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows; + +// #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; + +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..3855d5deb --- /dev/null +++ b/src/main/java/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.java @@ -0,0 +1,17 @@ +package g3701_3800.s3726_remove_zeros_in_decimal_representation; + +// #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) { + 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..83e32aaf8 --- /dev/null +++ b/src/main/java/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.java @@ -0,0 +1,28 @@ +package g3701_3800.s3727_maximum_alternating_sum_of_squares; + +// #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) { + int n = nums.length; + 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; + } + 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 total - 2 * small; + } +} 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..e9cf1f3c7 --- /dev/null +++ b/src/main/java/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.java @@ -0,0 +1,34 @@ +package g3701_3800.s3728_stable_subarrays_with_equal_boundary_and_interior_sum; + +// #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 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; + } + index++; + } + return ans; + } +} 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..06650ee14 --- /dev/null +++ b/src/main/java/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.java @@ -0,0 +1,31 @@ +package g3701_3800.s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array; + +// #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<>(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; + } + sum += x; + ans += cnt.getOrDefault((int) (sum % k), 0); + } + return ans; + } +} 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..0cf0d511c --- /dev/null +++ b/src/test/java/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.java @@ -0,0 +1,71 @@ +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("")); + } + + @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")); + } +} 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)); + } +}