Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/dbf48a81dddb02a4f7ba1716e478e498 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/dbf48a81dddb02a4f7ba1716e478e498 to your computer and use it in GitHub Desktop.
class FindSumPairs {
vector<int> arr1,arr2;
unordered_map<int,int> ele_freq;
public:
FindSumPairs(vector<int>& nums1, vector<int>& nums2) {
arr1 = nums1;
arr2 = nums2;
for(int ele: nums2)
ele_freq[ele]++;
}
void add(int index, int val) {
ele_freq[arr2[index]]--;
arr2[index] += val;
ele_freq[arr2[index]]++;
}
int count(int tot) {
int res = 0;
for(int ele: arr1)
if(ele_freq.count(tot-ele))
res += ele_freq[tot-ele];
return res;
}
};
/**
* 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);
*/
/*
//JAVA
import java.util.HashMap;
import java.util.Map;
class FindSumPairs {
private int[] arr1;
private int[] arr2;
private Map<Integer, Integer> eleFreq;
public FindSumPairs(int[] nums1, int[] nums2) {
this.arr1 = nums1;
this.arr2 = nums2.clone();
this.eleFreq = new HashMap<>();
for (int ele : nums2) {
eleFreq.put(ele, eleFreq.getOrDefault(ele, 0) + 1);
}
}
public void add(int index, int val) {
int oldVal = arr2[index];
eleFreq.put(oldVal, eleFreq.get(oldVal) - 1);
arr2[index] += val;
int newVal = arr2[index];
eleFreq.put(newVal, eleFreq.getOrDefault(newVal, 0) + 1);
}
public int count(int tot) {
int res = 0;
for (int ele : arr1) {
int target = tot - ele;
res += eleFreq.getOrDefault(target, 0);
}
return res;
}
}
#Python
from typing import List
from collections import defaultdict
class FindSumPairs:
def __init__(self, nums1: List[int], nums2: List[int]):
self.arr1 = nums1
self.arr2 = nums2.copy()
self.ele_freq = defaultdict(int)
for ele in nums2:
self.ele_freq[ele] += 1
def add(self, index: int, val: int) -> None:
old_val = self.arr2[index]
self.ele_freq[old_val] -= 1
self.arr2[index] += val
new_val = self.arr2[index]
self.ele_freq[new_val] += 1
def count(self, tot: int) -> int:
res = 0
for ele in self.arr1:
target = tot - ele
res += self.ele_freq.get(target, 0)
return res
*/
@JCSR2022
Copy link

JCSR2022 commented Jul 6, 2025

just an improve, you can do hash on nums1 too , i think it can be faster y there are a lot of repeated values on num1..

class FindSumPairs:

def __init__(self, nums1: List[int], nums2: List[int]):
    
    self.val_nums1 = {}
    for n1 in nums1:
        if n1 in self.val_nums1:
            self.val_nums1[n1] += 1
        else:
            self.val_nums1[n1] = 1

    self.nums2 = nums2
    self.val_nums2 = {}
    for n2 in self.nums2:
        if n2 in self.val_nums2:
            self.val_nums2[n2] +=1
        else:
            self.val_nums2[n2] = 1

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

def count(self, tot: int) -> int:
    ans = 0
    for n1,count_n1 in self.val_nums1.items():
        need_n2 = tot-n1
        if need_n2 in self.val_nums2:
            ans += count_n1*self.val_nums2[need_n2]
    return ans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment