Skip to content

Practice Finding Pairs With a Certain Sum

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1865
LeetCode Problem

Finding Pairs With a Certain Sum

Problem Statement

FindSumPairs(nums1,nums2) supports add(index,val), which increments nums2[index], and count(tot), which counts index pairs whose values sum to tot. nums1 remains fixed. Repeated values contribute multiple index pairs. Practice approach: Maintain frequencies for nums2. On update, decrement the old-value count and increment the new-value count. For each nums1 entry, add the frequency of tot minus that value. A count query is O(nums1.length), and updates are expected O(1). Do not confuse increment with replacement or discard duplicate pairs.

Source: Finding Pairs With a Certain Sum on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["FindSumPairs","count","add","count","count"]
[[[1,1,2],[3,4]],[5],[0,1],[5],[6]]
Output
[null,3,null,4,2]
Explanation

Initially two 1+4 pairs and one 2+3 pair sum to 5. Updating 3 to 4 yields four 1+4 pairs.

Constraints

1 <= nums1.length <= 1000
1 <= nums2.length <= 10^5
1 <= nums1[i], tot <= 10^9
1 <= nums2[i], val <= 10^5
0 <= index < nums2.length
At most 1000 calls to each method.

Related LeetCode Problems

Practice more problems similar to Finding Pairs With a Certain Sum.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.