Finding Pairs With a Certain Sum - Problem

You are given two integer arrays nums1 and nums2. You need to implement a data structure that supports queries of two types:

Add: Add a positive integer to an element at a given index in the array nums2.

Count: Count the number of pairs (i, j) such that nums1[i] + nums2[j] equals a given value (where 0 <= i < nums1.length and 0 <= j < nums2.length).

Implement the FindSumPairs class:

  • FindSumPairs(int[] nums1, int[] nums2) - Initializes the object with two integer arrays
  • void add(int index, int val) - Adds val to nums2[index]
  • int count(int tot) - Returns the number of pairs (i, j) such that nums1[i] + nums2[j] == tot

Input & Output

Example 1 — Basic Operations
$ Input: nums1 = [1,1,2,2,2,3], nums2 = [1,4,5,2,5,4], operations = [["count",7],["add",1,2],["count",8],["count",4],["add",2,1],["count",5]]
Output: [8,2,1,1]
💡 Note: count(7): pairs (0,2),(0,4),(1,2),(1,4),(2,1),(2,3),(4,1),(4,3) = 8 pairs. add(1,2): nums2[1] = 4+2 = 6. count(8): pairs (2,1),(4,1) = 2 pairs. count(4): pair (2,0) = 1 pair.
Example 2 — Simple Case
$ Input: nums1 = [1,2], nums2 = [3,4], operations = [["count",5],["add",0,1],["count",6]]
Output: [2,2]
💡 Note: count(5): pairs (0,1) and (1,0) where 1+4=5 and 2+3=5. add(0,1): nums2[0] = 3+1 = 4. count(6): pairs (0,1) and (1,0) where 1+5=6 and 2+4=6.
Example 3 — No Pairs Found
$ Input: nums1 = [1,2], nums2 = [5,6], operations = [["count",3],["add",0,2],["count",5]]
Output: [0,0]
💡 Note: count(3): no pairs sum to 3. add(0,2): nums2[0] = 5+2 = 7. count(5): still no pairs sum to 5.

Constraints

  • 1 ≤ nums1.length ≤ 1000
  • 1 ≤ nums2.length ≤ 105
  • 1 ≤ nums1[i] ≤ 109
  • 1 ≤ nums2[i] ≤ 109

Visualization

Tap to expand
Finding Pairs With a Certain Sum INPUT nums1: 1 1 2 2 2 3 nums2: 1 4 5 2 5 4 Operations: count(7) add(1, 2) count(8) count(4) add(2, 1) count(5) HashMap for nums2: {1:1, 4:2, 5:2, 2:1} value: count mapping ALGORITHM STEPS 1 Build HashMap Store nums2 freq counts 2 For count(tot): For each n1 in nums1 Find map[tot - n1] 3 For add(idx, val): Decrement old freq Increment new freq 4 Sum pairs count Accumulate matches Example: count(7) n1=1: need 6 --> 0 pairs n1=1: need 6 --> 0 pairs n1=2: need 5 --> 2 pairs n1=2: need 5 --> 2 pairs n1=2: need 5 --> 2 pairs n1=3: need 4 --> 2 pairs Total: 8 FINAL RESULT Output: 8 2 1 1 Step-by-step results: count(7) = 8 pairs add(1,2): nums2[1]=4+2=6 count(8) = 2 pairs count(4) = 1 pair add(2,1): nums2[2]=5+1=6 count(5) = 1 pair OK - All operations complete Key Insight: Use a HashMap to track frequency counts of nums2 values. For count queries, iterate through nums1 and lookup (target - nums1[i]) in the HashMap. For add operations, update the frequency map. Time: O(n) for count, O(1) for add | Space: O(m) for HashMap where m = nums2.length TutorialsPoint - Finding Pairs With a Certain Sum | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
18.6K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen