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


	Add a positive integer to an element of a given index in the array nums2.
	Count the number of pairs (i, j) such that nums1[i] + nums2[j] equals a given value (0 &lt;= i &lt; nums1.length and 0 &lt;= j &lt; nums2.length).


Implement the FindSumPairs class:


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


&nbsp;
Example 1:

Input
["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]
[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
Output
[null, 8, null, 2, 1, null, null, 11]

Explanation
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
findSumPairs.count(7);  // return 8; pairs (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) make 2 + 5 and pairs (5,1), (5,5) make 3 + 4
findSumPairs.add(3, 2); // now nums2 = [1,4,5,4,5,4]
findSumPairs.count(8);  // return 2; pairs (5,2), (5,4) make 3 + 5
findSumPairs.count(4);  // return 1; pair (5,0) makes 3 + 1
findSumPairs.add(0, 1); // now nums2 = [2,4,5,4,5,4]
findSumPairs.add(1, 1); // now nums2 = [2,5,5,4,5,4]
findSumPairs.count(7);  // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) make 2 + 5 and pairs (5,3), (5,5) make 3 + 4


&nbsp;
Constraints:


	1 &lt;= nums1.length &lt;= 1000
	1 &lt;= nums2.length &lt;= 105
	1 &lt;= nums1[i] &lt;= 109
	1 &lt;= nums2[i] &lt;= 105
	0 &lt;= index &lt; nums2.length
	1 &lt;= val &lt;= 105
	1 &lt;= tot &lt;= 109
	At most 1000 calls are made to add and count each.


class FindSumPairs:

    def __init__(self, nums1: List[int], nums2: List[int]):
        self.nums1 = sorted(nums1)
        self.nums2 = nums2
        self.hash2 = defaultdict(int)
        for n in nums2:
            self.hash2[n] += 1

    def add(self, index: int, val: int) -> None:
        self.hash2[self.nums2[index]] -= 1
        self.nums2[index] += val
        self.hash2[self.nums2[index]] += 1

    def count(self, tot: int) -> int:
        result = 0
        for n in self.nums1:
            if n >= tot:
                break
            result += self.hash2[tot - n]
        return result
class FindSumPairs {

    private int [] nums1;
    private int [] nums2;
    Map<Integer, Integer> map = new HashMap<>();
    public FindSumPairs(int[] nums1, int[] nums2) {
        this.nums1 = nums1;
        this.nums2 = nums2;
        for (int number : nums2) {
            map.put(number, map.getOrDefault(number, 0) + 1);
        } 
        
    }
    
    public void add(int index, int val) {
        map.put(nums2[index], map.get(nums2[index]) - 1);
        nums2[index] += val;
        map.put(nums2[index], map.getOrDefault(nums2[index], 0) + 1);
    }
    
    public int count(int tot) {
        int result = 0;
        for (int number : nums1) {
            if (map.containsKey(tot - number)) {
                result += map.get(tot - number);
            }
        }
        return result;
    }
}

/**
 * Your FindSumPairs object will be instantiated and called as such:
 * FindSumPairs obj = new FindSumPairs(nums1, nums2);
 * obj.add(index,val);
 * int param_2 = obj.count(tot);
 */
class FindSumPairs {
public:
    //use map to store the element as key and its frequency as its value
    unordered_map<int,int> freq;
    vector<int> v1;
    vector<int> v2;
    FindSumPairs(vector<int>& nums1, vector<int>& nums2) {
        for(auto ele:nums2)
            freq[ele]++;
        v1=nums1;
        v2=nums2;
    }
    
    void add(int index, int val) {
        //decrease the freq of value at the index
        freq[v2[index]]-- ;
        v2[index]+=val;
        //we got a new value, increment the freq of the value
        freq[v2[index]]++;
    }
    
    int count(int tot) {
        //traverese through the v1 and find the elements which sum upto the target tot
        int pairs=0;
        for(auto ele:v1)
            if(tot>ele && freq.count(tot-ele))
                pairs+=freq[tot-ele];
        return pairs;
    }
};

/**
 * Your FindSumPairs object will be instantiated and called as such:
 * FindSumPairs* obj = new FindSumPairs(nums1, nums2);
 * obj->add(index,val);
 * int param_2 = obj->count(tot);
 */
function Counter(arr) {

    // input: array
    // output: corresponding occurrence-based dictionary

    let keyOccDict = {};

    arr.forEach(val => keyOccDict[val] = (keyOccDict[val] || 0) + 1);

    return keyOccDict;
}

var FindSumPairs = function(nums1, nums2) {
    this.arrA = nums1;
    this.arrB = nums2;

    // maintain mapping between distinct number and occurrence for array A
    this.dictA = Counter(this.arrA );

    // maintain mapping between distinct number and occurrence for array B
    this.dictB = Counter(this.arrB );

    return
};

FindSumPairs.prototype.add = function(index, val) {

    // update mapping for arrB

    let old_value = this.arrB[index];
    this.arrB[index] += val;
    let new_value = this.arrB[index];

    this.dictB[ old_value ] = this.dictB[old_value] - 1;
    this.dictB[ new_value ] = (this.dictB[new_value] || 0) + 1;

    return
};

FindSumPairs.prototype.count = function(tot) {

    /*
      Goal:

      Find the method count of a + b = total,
      where a comes from array A, and b comes from array B

      apply the concept learned from Leetcode #1 Two sum
      a + b = total <=> b = total - a

      speed-up by dictionary
    */

    let counter = 0;

    for( const [a, occ_a] of Object.entries(this.dictA) ){

        let b = tot - a;

        counter += occ_a * (this.dictB[b] || 0);

    }

    return counter;
};
Finding Pairs With a Certain Sum