From ffd66ba57cc8cd55e69bc78c624b74f242487876 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 30 May 2024 03:22:26 +0300 Subject: [PATCH] Added tasks 3158-3162 --- .../Solution.java | 17 +++ .../readme.md | 43 +++++++ .../Solution.java | 22 ++++ .../readme.md | 38 +++++++ .../Solution.java | 32 ++++++ .../readme.md | 50 ++++++++ .../Solution.java | 107 ++++++++++++++++++ .../s3161_block_placement_queries/readme.md | 46 ++++++++ .../Solution.java | 17 +++ .../readme.md | 35 ++++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 23 ++++ .../SolutionTest.java | 31 +++++ .../SolutionTest.java | 33 ++++++ .../SolutionTest.java | 22 ++++ 15 files changed, 539 insertions(+) create mode 100644 src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/Solution.java create mode 100644 src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md create mode 100644 src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/Solution.java create mode 100644 src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md create mode 100644 src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/Solution.java create mode 100644 src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md create mode 100644 src/main/java/g3101_3200/s3161_block_placement_queries/Solution.java create mode 100644 src/main/java/g3101_3200/s3161_block_placement_queries/readme.md create mode 100644 src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/Solution.java create mode 100644 src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/readme.md create mode 100644 src/test/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3161_block_placement_queries/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/SolutionTest.java diff --git a/src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/Solution.java b/src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/Solution.java new file mode 100644 index 000000000..ca39574c7 --- /dev/null +++ b/src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/Solution.java @@ -0,0 +1,17 @@ +package g3101_3200.s3158_find_the_xor_of_numbers_which_appear_twice; + +// #Easy #Array #Hash_Table #Bit_Manipulation #2024_05_30_Time_1_ms_(99.87%)_Space_42.3_MB_(99.40%) + +public class Solution { + public int duplicateNumbersXOR(int[] nums) { + boolean[] appeared = new boolean[51]; + int res = 0; + for (int num : nums) { + if (appeared[num]) { + res ^= num; + } + appeared[num] = true; + } + return res; + } +} diff --git a/src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md b/src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md new file mode 100644 index 000000000..b0354efb1 --- /dev/null +++ b/src/main/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md @@ -0,0 +1,43 @@ +3158\. Find the XOR of Numbers Which Appear Twice + +Easy + +You are given an array `nums`, where each number in the array appears **either** once or twice. + +Return the bitwise `XOR` of all the numbers that appear twice in the array, or 0 if no number appears twice. + +**Example 1:** + +**Input:** nums = [1,2,1,3] + +**Output:** 1 + +**Explanation:** + +The only number that appears twice in `nums` is 1. + +**Example 2:** + +**Input:** nums = [1,2,3] + +**Output:** 0 + +**Explanation:** + +No number appears twice in `nums`. + +**Example 3:** + +**Input:** nums = [1,2,2,1] + +**Output:** 3 + +**Explanation:** + +Numbers 1 and 2 appeared twice. `1 XOR 2 == 3`. + +**Constraints:** + +* `1 <= nums.length <= 50` +* `1 <= nums[i] <= 50` +* Each number in `nums` appears either once or twice. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/Solution.java b/src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/Solution.java new file mode 100644 index 000000000..421139905 --- /dev/null +++ b/src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/Solution.java @@ -0,0 +1,22 @@ +package g3101_3200.s3159_find_occurrences_of_an_element_in_an_array; + +// #Medium #Array #Hash_Table #2024_05_30_Time_4_ms_(96.74%)_Space_64_MB_(69.94%) + +import java.util.ArrayList; + +public class Solution { + public int[] occurrencesOfElement(int[] nums, int[] queries, int x) { + ArrayList a = new ArrayList<>(); + for (int i = 0, l = nums.length; i < l; i++) { + if (nums[i] == x) { + a.add(i); + } + } + int l = queries.length; + int[] r = new int[l]; + for (int i = 0; i < l; i++) { + r[i] = queries[i] > a.size() ? -1 : a.get(queries[i] - 1); + } + return r; + } +} diff --git a/src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md b/src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md new file mode 100644 index 000000000..6f36cb2a5 --- /dev/null +++ b/src/main/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md @@ -0,0 +1,38 @@ +3159\. Find Occurrences of an Element in an Array + +Medium + +You are given an integer array `nums`, an integer array `queries`, and an integer `x`. + +For each `queries[i]`, you need to find the index of the queries[i]th occurrence of `x` in the `nums` array. If there are fewer than `queries[i]` occurrences of `x`, the answer should be -1 for that query. + +Return an integer array `answer` containing the answers to all queries. + +**Example 1:** + +**Input:** nums = [1,3,1,7], queries = [1,3,2,4], x = 1 + +**Output:** [0,-1,2,-1] + +**Explanation:** + +* For the 1st query, the first occurrence of 1 is at index 0. +* For the 2nd query, there are only two occurrences of 1 in `nums`, so the answer is -1. +* For the 3rd query, the second occurrence of 1 is at index 2. +* For the 4th query, there are only two occurrences of 1 in `nums`, so the answer is -1. + +**Example 2:** + +**Input:** nums = [1,2,3], queries = [10], x = 5 + +**Output:** [-1] + +**Explanation:** + +* For the 1st query, 5 doesn't exist in `nums`, so the answer is -1. + +**Constraints:** + +* 1 <= nums.length, queries.length <= 105 +* 1 <= queries[i] <= 105 +* 1 <= nums[i], x <= 104 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/Solution.java b/src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/Solution.java new file mode 100644 index 000000000..09b563255 --- /dev/null +++ b/src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/Solution.java @@ -0,0 +1,32 @@ +package g3101_3200.s3160_find_the_number_of_distinct_colors_among_the_balls; + +// #Medium #Array #Hash_Table #Simulation #2024_05_30_Time_36_ms_(98.82%)_Space_79.6_MB_(93.03%) + +import java.util.HashMap; +import java.util.Map; + +@SuppressWarnings("java:S1172") +public class Solution { + public int[] queryResults(int ignoredLimit, int[][] queries) { + Map ballToColor = new HashMap<>(); + Map colorToCnt = new HashMap<>(); + int[] ret = new int[queries.length]; + for (int i = 0; i < queries.length; i += 1) { + final int ball = queries[i][0]; + final int color = queries[i][1]; + if (ballToColor.containsKey(ball)) { + int oldColor = ballToColor.get(ball); + int oldColorCnt = colorToCnt.get(oldColor); + if (oldColorCnt >= 2) { + colorToCnt.put(oldColor, oldColorCnt - 1); + } else { + colorToCnt.remove(oldColor); + } + } + ballToColor.put(ball, color); + colorToCnt.put(color, colorToCnt.getOrDefault(color, 0) + 1); + ret[i] = colorToCnt.size(); + } + return ret; + } +} diff --git a/src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md b/src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md new file mode 100644 index 000000000..f132574a7 --- /dev/null +++ b/src/main/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md @@ -0,0 +1,50 @@ +3160\. Find the Number of Distinct Colors Among the Balls + +Medium + +You are given an integer `limit` and a 2D array `queries` of size `n x 2`. + +There are `limit + 1` balls with **distinct** labels in the range `[0, limit]`. Initially, all balls are uncolored. For every query in `queries` that is of the form `[x, y]`, you mark ball `x` with the color `y`. After each query, you need to find the number of **distinct** colors among the balls. + +Return an array `result` of length `n`, where `result[i]` denotes the number of distinct colors _after_ ith query. + +**Note** that when answering a query, lack of a color _will not_ be considered as a color. + +**Example 1:** + +**Input:** limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]] + +**Output:** [1,2,2,3] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop.gif) + +* After query 0, ball 1 has color 4. +* After query 1, ball 1 has color 4, and ball 2 has color 5. +* After query 2, ball 1 has color 3, and ball 2 has color 5. +* After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4. + +**Example 2:** + +**Input:** limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]] + +**Output:** [1,2,2,3,4] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop2.gif)** + +* After query 0, ball 0 has color 1. +* After query 1, ball 0 has color 1, and ball 1 has color 2. +* After query 2, ball 0 has color 1, and balls 1 and 2 have color 2. +* After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4. +* After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5. + +**Constraints:** + +* 1 <= limit <= 109 +* 1 <= n == queries.length <= 105 +* `queries[i].length == 2` +* `0 <= queries[i][0] <= limit` +* 1 <= queries[i][1] <= 109 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3161_block_placement_queries/Solution.java b/src/main/java/g3101_3200/s3161_block_placement_queries/Solution.java new file mode 100644 index 000000000..3bd92d82e --- /dev/null +++ b/src/main/java/g3101_3200/s3161_block_placement_queries/Solution.java @@ -0,0 +1,107 @@ +package g3101_3200.s3161_block_placement_queries; + +// #Hard #Array #Binary_Search #Segment_Tree #Binary_Indexed_Tree +// #2024_05_30_Time_137_ms_(99.38%)_Space_143.7_MB_(54.52%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + private static class Seg { + private final int start; + private final int end; + private int min; + private int max; + private int len; + private boolean obstacle; + private Seg left; + private Seg right; + + public static Seg init(int n) { + return new Seg(0, n); + } + + private Seg(int start, int end) { + this.start = start; + this.end = end; + if (start >= end) { + return; + } + int mid = start + ((end - start) >> 1); + left = new Seg(start, mid); + right = new Seg(mid + 1, end); + refresh(); + } + + public void set(int i) { + if (i < start || i > end) { + return; + } else if (i == start && i == end) { + obstacle = true; + min = max = start; + return; + } + left.set(i); + right.set(i); + refresh(); + } + + private void refresh() { + if (left.obstacle) { + min = left.min; + if (right.obstacle) { + max = right.max; + len = Math.max(right.min - left.max, Math.max(left.len, right.len)); + } else { + max = left.max; + len = Math.max(left.len, right.end - left.max); + } + obstacle = true; + } else if (right.obstacle) { + min = right.min; + max = right.max; + len = Math.max(right.len, right.min - left.start); + obstacle = true; + } else { + len = end - start; + } + } + + public void max(int n, int[] t) { + if (end <= n) { + t[0] = Math.max(t[0], len); + if (obstacle) { + t[1] = max; + } + return; + } + left.max(n, t); + if (!right.obstacle || right.min >= n) { + return; + } + t[0] = Math.max(t[0], right.min - t[1]); + right.max(n, t); + } + } + + public List getResults(int[][] queries) { + int max = 0; + for (int[] i : queries) { + max = Math.max(max, i[1]); + } + Seg root = Seg.init(max); + root.set(0); + + List res = new ArrayList<>(queries.length); + for (int[] i : queries) { + if (i[0] == 1) { + root.set(i[1]); + } else { + int[] t = new int[2]; + root.max(i[1], t); + res.add(Math.max(t[0], i[1] - t[1]) >= i[2]); + } + } + return res; + } +} diff --git a/src/main/java/g3101_3200/s3161_block_placement_queries/readme.md b/src/main/java/g3101_3200/s3161_block_placement_queries/readme.md new file mode 100644 index 000000000..6775e99d0 --- /dev/null +++ b/src/main/java/g3101_3200/s3161_block_placement_queries/readme.md @@ -0,0 +1,46 @@ +3161\. Block Placement Queries + +Hard + +There exists an infinite number line, with its origin at 0 and extending towards the **positive** x-axis. + +You are given a 2D array `queries`, which contains two types of queries: + +1. For a query of type 1, `queries[i] = [1, x]`. Build an obstacle at distance `x` from the origin. It is guaranteed that there is **no** obstacle at distance `x` when the query is asked. +2. For a query of type 2, `queries[i] = [2, x, sz]`. Check if it is possible to place a block of size `sz` _anywhere_ in the range `[0, x]` on the line, such that the block **entirely** lies in the range `[0, x]`. A block **cannot** be placed if it intersects with any obstacle, but it may touch it. Note that you do **not** actually place the block. Queries are separate. + +Return a boolean array `results`, where `results[i]` is `true` if you can place the block specified in the ith query of type 2, and `false` otherwise. + +**Example 1:** + +**Input:** queries = [[1,2],[2,3,3],[2,3,1],[2,2,2]] + +**Output:** [false,true,true] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/22/example0block.png)** + +For query 0, place an obstacle at `x = 2`. A block of size at most 2 can be placed before `x = 3`. + +**Example 2:** + +**Input:** queries = [[1,7],[2,7,6],[1,2],[2,7,5],[2,7,6]] + +**Output:** [true,true,false] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/22/example1block.png)** + +* Place an obstacle at `x = 7` for query 0. A block of size at most 7 can be placed before `x = 7`. +* Place an obstacle at `x = 2` for query 2. Now, a block of size at most 5 can be placed before `x = 7`, and a block of size at most 2 before `x = 2`. + +**Constraints:** + +* 1 <= queries.length <= 15 * 104 +* `2 <= queries[i].length <= 3` +* `1 <= queries[i][0] <= 2` +* 1 <= x, sz <= min(5 * 104, 3 * queries.length) +* The input is generated such that for queries of type 1, no obstacle exists at distance `x` when the query is asked. +* The input is generated such that there is at least one query of type 2. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/Solution.java b/src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/Solution.java new file mode 100644 index 000000000..ce2aba1d2 --- /dev/null +++ b/src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/Solution.java @@ -0,0 +1,17 @@ +package g3101_3200.s3162_find_the_number_of_good_pairs_i; + +// #Easy #Array #Hash_Table #2024_05_30_Time_1_ms_(99.96%)_Space_42.1_MB_(99.36%) + +public class Solution { + public int numberOfPairs(int[] nums1, int[] nums2, int k) { + int c = 0; + for (int j : nums1) { + for (int value : nums2) { + if (j % (value * k) == 0) { + c++; + } + } + } + return c; + } +} diff --git a/src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/readme.md b/src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/readme.md new file mode 100644 index 000000000..57827a23f --- /dev/null +++ b/src/main/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/readme.md @@ -0,0 +1,35 @@ +3162\. Find the Number of Good Pairs I + +Easy + +You are given 2 integer arrays `nums1` and `nums2` of lengths `n` and `m` respectively. You are also given a **positive** integer `k`. + +A pair `(i, j)` is called **good** if `nums1[i]` is divisible by `nums2[j] * k` (`0 <= i <= n - 1`, `0 <= j <= m - 1`). + +Return the total number of **good** pairs. + +**Example 1:** + +**Input:** nums1 = [1,3,4], nums2 = [1,3,4], k = 1 + +**Output:** 5 + +**Explanation:** + +The 5 good pairs are `(0, 0)`, `(1, 0)`, `(1, 1)`, `(2, 0)`, and `(2, 2)`. + +**Example 2:** + +**Input:** nums1 = [1,2,4,12], nums2 = [2,4], k = 3 + +**Output:** 2 + +**Explanation:** + +The 2 good pairs are `(3, 0)` and `(3, 1)`. + +**Constraints:** + +* `1 <= n, m <= 50` +* `1 <= nums1[i], nums2[j] <= 50` +* `1 <= k <= 50` \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/SolutionTest.java b/src/test/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/SolutionTest.java new file mode 100644 index 000000000..0dbc50247 --- /dev/null +++ b/src/test/java/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/SolutionTest.java @@ -0,0 +1,23 @@ +package g3101_3200.s3158_find_the_xor_of_numbers_which_appear_twice; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void duplicateNumbersXOR() { + assertThat(new Solution().duplicateNumbersXOR(new int[] {1, 2, 1, 3}), equalTo(1)); + } + + @Test + void duplicateNumbersXOR2() { + assertThat(new Solution().duplicateNumbersXOR(new int[] {1, 2, 3}), equalTo(0)); + } + + @Test + void duplicateNumbersXOR3() { + assertThat(new Solution().duplicateNumbersXOR(new int[] {1, 2, 2, 1}), equalTo(3)); + } +} diff --git a/src/test/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/SolutionTest.java b/src/test/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/SolutionTest.java new file mode 100644 index 000000000..cfb781eac --- /dev/null +++ b/src/test/java/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/SolutionTest.java @@ -0,0 +1,23 @@ +package g3101_3200.s3159_find_occurrences_of_an_element_in_an_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void occurrencesOfElement() { + assertThat( + new Solution() + .occurrencesOfElement(new int[] {1, 3, 1, 7}, new int[] {1, 3, 2, 4}, 1), + equalTo(new int[] {0, -1, 2, -1})); + } + + @Test + void occurrencesOfElement2() { + assertThat( + new Solution().occurrencesOfElement(new int[] {1, 2, 3}, new int[] {10}, 5), + equalTo(new int[] {-1})); + } +} diff --git a/src/test/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/SolutionTest.java b/src/test/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/SolutionTest.java new file mode 100644 index 000000000..ae6576ed5 --- /dev/null +++ b/src/test/java/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/SolutionTest.java @@ -0,0 +1,31 @@ +package g3101_3200.s3160_find_the_number_of_distinct_colors_among_the_balls; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void queryResults() { + assertThat( + new Solution().queryResults(4, new int[][] {{1, 4}, {2, 5}, {1, 3}, {3, 4}}), + equalTo(new int[] {1, 2, 2, 3})); + } + + @Test + void queryResults2() { + assertThat( + new Solution() + .queryResults(4, new int[][] {{0, 1}, {1, 2}, {2, 2}, {3, 4}, {4, 5}}), + equalTo(new int[] {1, 2, 2, 3, 4})); + } + + @Test + void queryResults3() { + assertThat( + new Solution() + .queryResults(1, new int[][] {{0, 2}, {1, 10}, {0, 10}, {0, 3}, {1, 5}}), + equalTo(new int[] {1, 2, 1, 2, 2})); + } +} diff --git a/src/test/java/g3101_3200/s3161_block_placement_queries/SolutionTest.java b/src/test/java/g3101_3200/s3161_block_placement_queries/SolutionTest.java new file mode 100644 index 000000000..eb9bc6169 --- /dev/null +++ b/src/test/java/g3101_3200/s3161_block_placement_queries/SolutionTest.java @@ -0,0 +1,33 @@ +package g3101_3200.s3161_block_placement_queries; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void getResults() { + assertThat( + new Solution().getResults(new int[][] {{1, 2}, {2, 3, 3}, {2, 3, 1}, {2, 2, 2}}), + equalTo(List.of(false, true, true))); + } + + @Test + void getResults2() { + assertThat( + new Solution() + .getResults(new int[][] {{1, 7}, {2, 7, 6}, {1, 2}, {2, 7, 5}, {2, 7, 6}}), + equalTo(List.of(true, true, false))); + } + + @Test + void getResults3() { + assertThat( + new Solution() + .getResults( + new int[][] {{1, 4}, {1, 9}, {2, 15, 4}, {2, 11, 6}, {2, 13, 10}}), + equalTo(List.of(true, false, false))); + } +} diff --git a/src/test/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/SolutionTest.java b/src/test/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/SolutionTest.java new file mode 100644 index 000000000..5028d19e2 --- /dev/null +++ b/src/test/java/g3101_3200/s3162_find_the_number_of_good_pairs_i/SolutionTest.java @@ -0,0 +1,22 @@ +package g3101_3200.s3162_find_the_number_of_good_pairs_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numberOfPairs() { + assertThat( + new Solution().numberOfPairs(new int[] {1, 3, 4}, new int[] {1, 3, 4}, 1), + equalTo(5)); + } + + @Test + void numberOfPairs2() { + assertThat( + new Solution().numberOfPairs(new int[] {1, 2, 4, 12}, new int[] {2, 4}, 3), + equalTo(2)); + } +}