1
+ package array .pairs_with_certian_sum ;
2
+
3
+ import org .junit .jupiter .api .DisplayName ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
7
+
8
+ /**
9
+ * Test suite for the {@link FrequencyMap} class.
10
+ */
11
+ class FrequencyMapTest {
12
+
13
+ @ Test
14
+ @ DisplayName ("Should correctly handle the provided LeetCode example scenario" )
15
+ void shouldPassLeetCodeScenario () {
16
+ // Input:
17
+ // ["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]
18
+ // [[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
19
+ // Expected Output:
20
+ // [null, 8, null, 2, 1, null, null, 11]
21
+
22
+ // Initialize the class
23
+ int [] nums1 = {1 , 1 , 2 , 2 , 2 , 3 };
24
+ int [] nums2 = {1 , 4 , 5 , 2 , 5 , 4 };
25
+ FrequencyMap findSumPairs = new FrequencyMap (nums1 , nums2 );
26
+
27
+ // count(7) -> Expected: 8
28
+ // Pairs: (2,5), (2,5), (2,5), (2,5), (2,5), (2,5) from nums1[2,3,4] and nums2[2,4] -> 3*2=6
29
+ // Pairs: (3,4), (3,4) from nums1[5] and nums2[1,5] -> 1*2=2. Total = 8.
30
+ assertEquals (8 , findSumPairs .count (7 ), "Initial count for total 7 should be 8" );
31
+
32
+ // add(3, 2) -> nums2 becomes [1, 4, 5, 4, 5, 4]
33
+ findSumPairs .add (3 , 2 );
34
+
35
+ // count(8) -> Expected: 2
36
+ // Pairs: (3,5), (3,5) from nums1[5] and nums2[2,4] -> 1*2=2. Total = 2.
37
+ assertEquals (2 , findSumPairs .count (8 ), "Count for total 8 after first add should be 2" );
38
+
39
+ // count(4) -> Expected: 1
40
+ // Pair: (3,1) from nums1[5] and nums2[0]. Total = 1.
41
+ assertEquals (1 , findSumPairs .count (4 ), "Count for total 4 should be 1" );
42
+
43
+ // add(0, 1) -> nums2 becomes [2, 4, 5, 4, 5, 4]
44
+ findSumPairs .add (0 , 1 );
45
+ // add(1, 1) -> nums2 becomes [2, 5, 5, 4, 5, 4]
46
+ findSumPairs .add (1 , 1 );
47
+
48
+ // count(7) -> Expected: 11
49
+ // Pairs: (2,5), (2,5), (2,5) from nums1[2,3,4] and nums2[1,2,4] -> 3*3=9
50
+ // Pairs: (3,4), (3,4) from nums1[5] and nums2[3,5] -> 1*2=2. Total = 11.
51
+ assertEquals (11 , findSumPairs .count (7 ), "Count for total 7 after final adds should be 11" );
52
+ }
53
+
54
+ @ Test
55
+ @ DisplayName ("Should return 0 when no pairs sum to the target" )
56
+ void shouldReturnZeroWhenNoPairsMatch () {
57
+ int [] nums1 = {10 , 20 , 30 };
58
+ int [] nums2 = {1 , 2 , 3 };
59
+ FrequencyMap findSumPairs = new FrequencyMap (nums1 , nums2 );
60
+
61
+ assertEquals (0 , findSumPairs .count (100 ), "Should return 0 for a total that cannot be formed" );
62
+ assertEquals (0 , findSumPairs .count (5 ), "Should return 0 for a total that is too small" );
63
+ }
64
+
65
+ @ Test
66
+ @ DisplayName ("Should correctly update counts after an add operation" )
67
+ void shouldUpdateCountsAfterAdd () {
68
+ int [] nums1 = {1 , 2 , 3 };
69
+ int [] nums2 = {1 , 1 , 1 };
70
+ FrequencyMap findSumPairs = new FrequencyMap (nums1 , nums2 );
71
+
72
+ // Initially, 3 pairs sum to 2: (1,1), (1,1), (1,1)
73
+ assertEquals (3 , findSumPairs .count (2 ), "Initial count for total 2 should be 3" );
74
+
75
+ // Add 1 to nums2[0], making it [2, 1, 1]
76
+ findSumPairs .add (0 , 1 );
77
+
78
+ // Now, 2 pairs sum to 2: (1,1), (1,1)
79
+ assertEquals (2 , findSumPairs .count (2 ), "Count for total 2 should decrease to 2 after add" );
80
+ // And 1 pair sums to 4: (2,2), (3, 1), (3, 1)
81
+ assertEquals (3 , findSumPairs .count (4 ), "Count for total 4 should become 1 after add" );
82
+ }
83
+
84
+ @ Test
85
+ @ DisplayName ("Should handle arrays with duplicate numbers correctly" )
86
+ void shouldHandleDuplicatesInBothArrays () {
87
+ int [] nums1 = {2 , 2 , 2 };
88
+ int [] nums2 = {3 , 3 };
89
+ FrequencyMap findSumPairs = new FrequencyMap (nums1 , nums2 );
90
+
91
+ // Each of the 3 '2's in nums1 can be paired with each of the 2 '3's in nums2
92
+ assertEquals (6 , findSumPairs .count (5 ), "Should be 3 * 2 = 6 pairs that sum to 5" );
93
+ }
94
+ }
0 commit comments