diff --git a/src/main/java/g3901_4000/s3917_count_indices_with_opposite_parity/Solution.java b/src/main/java/g3901_4000/s3917_count_indices_with_opposite_parity/Solution.java new file mode 100644 index 000000000..fb959f75b --- /dev/null +++ b/src/main/java/g3901_4000/s3917_count_indices_with_opposite_parity/Solution.java @@ -0,0 +1,23 @@ +package g3901_4000.s3917_count_indices_with_opposite_parity; + +// #Easy #Array #Mid_Level #Weekly_Contest_500 +// #2026_09_15_Time_1_ms_(100.00%)_Space_46.74_MB_(66.61%) + +public class Solution { + public int[] countOppositeParity(int[] nums) { + int n = nums.length; + int odd = 0; + int even = 0; + int[] result = new int[n]; + for (int i = n - 1; i >= 0; i--) { + if ((nums[i] & 1) == 1) { + result[i] = even; + odd++; + } else { + result[i] = odd; + even++; + } + } + return result; + } +} diff --git a/src/main/java/g3901_4000/s3917_count_indices_with_opposite_parity/readme.md b/src/main/java/g3901_4000/s3917_count_indices_with_opposite_parity/readme.md new file mode 100644 index 000000000..06118743d --- /dev/null +++ b/src/main/java/g3901_4000/s3917_count_indices_with_opposite_parity/readme.md @@ -0,0 +1,42 @@ +3917\. Count Indices With Opposite Parity + +Easy + +You are given an integer array `nums` of length `n`. + +The **score** of an index `i` is defined as the number of indices `j` such that: + +* `i < j < n`, and +* `nums[i]` and `nums[j]` have different parity (one is even and the other is odd). + +Return an integer array `answer` of length `n`, where `answer[i]` is the score of index `i`. + +**Example 1:** + +**Input:** nums = [1,2,3,4] + +**Output:** [2,1,1,0] + +**Explanation:** + +* `nums[0] = 1`, which is odd. Thus, the indices `j = 1` and `j = 3` satisfy the conditions, so the score of index 0 is 2. +* `nums[1] = 2`, which is even. Thus, the index `j = 2` satisfies the conditions, so the score of index 1 is 1. +* `nums[2] = 3`, which is odd. Thus, the index `j = 3` satisfies the conditions, so the score of index 2 is 1. +* `nums[3] = 4`, which is even. Thus, no index satisfies the conditions, so the score of index 3 is 0. + +Thus, the `answer = [2, 1, 1, 0]`. + +**Example 2:** + +**Input:** nums = [1] + +**Output:** [0] + +**Explanation:** + +There is only one element in `nums`. Thus, the score of index 0 is 0. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/Solution.java b/src/main/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/Solution.java new file mode 100644 index 000000000..a557ad120 --- /dev/null +++ b/src/main/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/Solution.java @@ -0,0 +1,46 @@ +package g3901_4000.s3918_sum_of_primes_between_number_and_its_reverse; + +// #Medium #Math #Number_Theory #Senior #Weekly_Contest_500 +// #2026_09_15_Time_3_ms_(96.45%)_Space_42.41_MB_(83.95%) + +public class Solution { + private boolean isPrime(int x) { + if (x <= 1) { + return false; + } + if (x == 2) { + return true; + } + if (x % 2 == 0) { + return false; + } + for (int i = 3; i * i <= x; i += 2) { + if (x % i == 0) { + return false; + } + } + return true; + } + + private int reverseNum(int n) { + int r = 0; + while (n > 0) { + r = r * 10 + (n % 10); + n /= 10; + } + return r; + } + + public int sumOfPrimesInRange(int n) { + int r = reverseNum(n); + int low = Math.min(n, r); + int high = Math.max(n, r); + int sum = 0; + for (int i = low; i <= high; i++) { + if (isPrime(i)) { + sum += i; + } + } + return sum; + } +} diff --git a/src/main/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/readme.md b/src/main/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/readme.md new file mode 100644 index 000000000..4e73cd7c7 --- /dev/null +++ b/src/main/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/readme.md @@ -0,0 +1,48 @@ +3918\. Sum of Primes Between Number and Its Reverse + +Medium + +You are given an integer `n`. + +Let `r` be the integer formed by reversing the digits of `n`. + +Return the **sum** of all prime numbers between `min(n, r)` and `max(n, r)`, inclusive. + +**Example 1:** + +**Input:** n = 13 + +**Output:** 132 + +**Explanation:** + +* The reverse of 13 is 31. Thus, the range is `[13, 31]`. +* The prime numbers in this range are 13, 17, 19, 23, 29, and 31. +* The sum of these prime numbers is `13 + 17 + 19 + 23 + 29 + 31 = 132`. + +**Example 2:** + +**Input:** n = 10 + +**Output:** 17 + +**Explanation:** + +* The reverse of 10 is 1. Thus, the range is `[1, 10]`. +* The prime numbers in this range are 2, 3, 5, and 7. +* The sum of these prime numbers is `2 + 3 + 5 + 7 = 17`. + +**Example 3:** + +**Input:** n = 8 + +**Output:** 0 + +**Explanation:** + +* The reverse of 8 is 8. Thus, the range is `[8, 8]`. +* There are no prime numbers in this range, so the sum is 0. + +**Constraints:** + +* `1 <= n <= 1000` \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/Solution.java b/src/main/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/Solution.java new file mode 100644 index 000000000..8bd975594 --- /dev/null +++ b/src/main/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/Solution.java @@ -0,0 +1,37 @@ +package g3901_4000.s3919_minimum_cost_to_move_between_indices; + +// #Medium #Array #Greedy #Prefix_Sum #Staff #Weekly_Contest_500 +// #2026_09_15_Time_4_ms_(100.00%)_Space_187.52_MB_(54.12%) + +public class Solution { + public int[] minCost(int[] nums, int[][] queries) { + int n = nums.length; + int[] prefixSum = new int[n]; + int[] suffixSum = new int[n]; + prefixSum[1] = 1; + for (int i = 1; i < n - 1; i++) { + int left = Math.abs(nums[i] - nums[i - 1]); + int right = Math.abs(nums[i] - nums[i + 1]); + if (left <= right) { + prefixSum[i + 1] = prefixSum[i] + right; + suffixSum[i] = suffixSum[i - 1] + 1; + } else { + prefixSum[i + 1] = prefixSum[i] + 1; + suffixSum[i] = suffixSum[i - 1] + left; + } + } + suffixSum[n - 1] = suffixSum[n - 2] + 1; + int[] ans = new int[queries.length]; + int i = 0; + for (int[] qur : queries) { + int l = qur[0]; + int r = qur[1]; + if (l > r) { + ans[i++] = suffixSum[l] - suffixSum[r]; + } else { + ans[i++] = prefixSum[r] - prefixSum[l]; + } + } + return ans; + } +} diff --git a/src/main/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/readme.md b/src/main/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/readme.md new file mode 100644 index 000000000..26a49ece6 --- /dev/null +++ b/src/main/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/readme.md @@ -0,0 +1,59 @@ +3919\. Minimum Cost to Move Between Indices + +Medium + +You are given an integer array `nums` where `nums` is **strictly increasing**. + +For each index `x`, let `closest(x)` be the **adjacent** index `y` such that `abs(nums[x] - nums[y])` is **minimized**. If both **adjacent** indices exist and give the same difference, choose the **smaller** index. + +From any index `x`, you can move in two ways: + +* To any index `y` with cost `abs(nums[x] - nums[y])`, or +* To `closest(x)` with cost 1. + +You are also given a 2D integer array `queries`, where each queries[i] = [li, ri]. + +For each query, calculate the **minimum total cost** to move from index li to index ri. + +Return an integer array `ans`, where `ans[i]` is the answer for the ith query. + +The **absolute difference** between two values `x` and `y` is defined as `abs(x - y)`. + +**Example 1:** + +**Input:** nums = [-5,-2,3], queries = [[0,2],[2,0],[1,2]] + +**Output:** [6,2,5] + +**Explanation:** + +* The closest indices are `[1, 0, 1]` respectively. +* For `[0, 2]`, the path `0 → 1 → 2` uses a closest move from index 0 to 1 with cost 1 and a move from index 1 to 2 with cost `|-2 - 3| = 5`, giving total `1 + 5 = 6`. +* For `[2, 0]`, the path `2 → 1 → 0` uses two closest moves from index 2 to 1 and from index 1 to 0, each with cost 1, giving total 2. +* For `[1, 2]`, the direct move from index 1 to index 2 has cost `|-2 - 3| = 5`, which is optimal. + +Thus, `ans = [6, 2, 5]`. + +**Example 2:** + +**Input:** nums = [0,2,3,9], queries = [[3,0],[1,2],[2,0]] + +**Output:** [4,1,3] + +**Explanation:** + +* The closest indices are `[1, 2, 1, 2]` respectively. +* For `[3, 0]`, the path `3 → 2 → 1 → 0` uses closest moves from index 3 to 2 and from 2 to 1, each with cost 1, and a move from 1 to 0 with cost `|2 - 0| = 2`, giving total `1 + 1 + 2 = 4`. +* For `[1, 2]`, the closest move from index 1 to 2 has cost 1. +* For `[2, 0]`, the path `2 → 1 → 0` uses a closest move from index 2 to 1 with cost 1 and a move from 1 to 0 with cost `|2 - 0| = 2`, giving total `1 + 2 = 3`. + +Thus, `ans = [4, 1, 3]`. + +**Constraints:** + +* 2 <= nums.length <= 105 +* -109 <= nums[i] <= 109 +* `nums` is strictly increasing +* 1 <= queries.length <= 105 +* queries[i] = [li, ri] +* 0 <= li, ri < nums.length \ No newline at end of file diff --git a/src/test/java/g3901_4000/s3917_count_indices_with_opposite_parity/SolutionTest.java b/src/test/java/g3901_4000/s3917_count_indices_with_opposite_parity/SolutionTest.java new file mode 100644 index 000000000..92e6647c5 --- /dev/null +++ b/src/test/java/g3901_4000/s3917_count_indices_with_opposite_parity/SolutionTest.java @@ -0,0 +1,20 @@ +package g3901_4000.s3917_count_indices_with_opposite_parity; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countOppositeParity() { + assertThat( + new Solution().countOppositeParity(new int[] {1, 2, 3, 4}), + equalTo(new int[] {2, 1, 1, 0})); + } + + @Test + void countOppositeParity2() { + assertThat(new Solution().countOppositeParity(new int[] {1}), equalTo(new int[] {0})); + } +} diff --git a/src/test/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/SolutionTest.java b/src/test/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/SolutionTest.java new file mode 100644 index 000000000..3af9381e8 --- /dev/null +++ b/src/test/java/g3901_4000/s3918_sum_of_primes_between_number_and_its_reverse/SolutionTest.java @@ -0,0 +1,23 @@ +package g3901_4000.s3918_sum_of_primes_between_number_and_its_reverse; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sumOfPrimesInRange() { + assertThat(new Solution().sumOfPrimesInRange(13), equalTo(132)); + } + + @Test + void sumOfPrimesInRange2() { + assertThat(new Solution().sumOfPrimesInRange(10), equalTo(17)); + } + + @Test + void sumOfPrimesInRange3() { + assertThat(new Solution().sumOfPrimesInRange(8), equalTo(0)); + } +} diff --git a/src/test/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/SolutionTest.java b/src/test/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/SolutionTest.java new file mode 100644 index 000000000..6889afc70 --- /dev/null +++ b/src/test/java/g3901_4000/s3919_minimum_cost_to_move_between_indices/SolutionTest.java @@ -0,0 +1,23 @@ +package g3901_4000.s3919_minimum_cost_to_move_between_indices; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minCost() { + assertThat( + new Solution().minCost(new int[] {-5, -2, 3}, new int[][] {{0, 2}, {2, 0}, {1, 2}}), + equalTo(new int[] {6, 2, 5})); + } + + @Test + void minCost2() { + assertThat( + new Solution() + .minCost(new int[] {0, 2, 3, 9}, new int[][] {{3, 0}, {1, 2}, {2, 0}}), + equalTo(new int[] {4, 1, 3})); + } +}