Created
July 6, 2025 03:59
-
-
Save SuryaPratapK/dbf48a81dddb02a4f7ba1716e478e498 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: