---
# System prepended metadata

title: 1865. Finding Pairs With a Certain Sum
tags: [Leetcode, Two Sum, Bloomberg, Medium]

---

# 1865. Finding Pairs With a Certain Sum
###### tags: `Leetcode` `Medium` `Bloomberg` `Two Sum`
Link: https://leetcode.com/problems/finding-pairs-with-a-certain-sum/
## 思路
two sum的方法
其实可以只用一个map和一个arr
但是懒得改了XD
## Code
```java=
class FindSumPairs {
    Map<Integer, Integer> nums1 = new HashMap<>();
    Map<Integer, Integer> nums2 = new HashMap<>();
    int[] arr;
    public FindSumPairs(int[] nums1, int[] nums2) {
        for(int i = 0;i < nums1.length;i++){
            this.nums1.put(nums1[i],this.nums1.getOrDefault(nums1[i],0)+1);
        }
        for(int i = 0;i < nums2.length;i++){
            this.nums2.put(nums2[i],this.nums2.getOrDefault(nums2[i],0)+1);
        }
        this.arr = nums2;
    }
    
    public void add(int index, int val) {
        int preVal = arr[index];
        this.nums2.put(preVal, this.nums2.get(preVal)-1);
        if(this.nums2.get(preVal) == 0) nums2.remove(preVal);
        arr[index] += val;
        this.nums2.put(arr[index], this.nums2.getOrDefault(arr[index],0)+1);
    }
    
    public int count(int tot) {
        int ans = 0;
        for(int key:nums1.keySet()){
            if(nums2.containsKey(tot-key)){
                ans += nums1.get(key)*nums2.get(tot-key);
            }
        }
        return ans;
    }
}

```