From 2d0b0ec9b15aaf8775beebc3e98c85d4b311b012 Mon Sep 17 00:00:00 2001 From: chayan das Date: Sun, 6 Jul 2025 11:06:33 +0530 Subject: [PATCH] Create 1865. Finding Pairs With a Certain Sum --- 1865. Finding Pairs With a Certain Sum | 60 ++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 1865. Finding Pairs With a Certain Sum diff --git a/1865. Finding Pairs With a Certain Sum b/1865. Finding Pairs With a Certain Sum new file mode 100644 index 0000000..c9d5a12 --- /dev/null +++ b/1865. Finding Pairs With a Certain Sum @@ -0,0 +1,60 @@ +class FindSumPairs { +private: + vector nums1; // Store nums1 as-is (never changes) + vector nums2; // Store nums2 for index-based updates + unordered_map freq; // Frequency map for nums2 values + +public: + FindSumPairs(vector& nums1, vector& nums2) { + // Store both arrays + this->nums1 = nums1; + this->nums2 = nums2; + + // Build frequency map for nums2 + // This helps us quickly find how many times each value appears + for (int num : nums2) { + freq[num]++; + } + } + + void add(int index, int val) { + // Get the old value at this index + int oldVal = nums2[index]; + + // Decrease frequency of old value + freq[oldVal]--; + if (freq[oldVal] == 0) { + freq.erase(oldVal); // Remove if frequency becomes 0 + } + + // Update nums2 with new value + nums2[index] += val; + int newVal = nums2[index]; + + // Increase frequency of new value + freq[newVal]++; + } + + int count(int tot) { + int pairCount = 0; + + // For each element in nums1, find its complement in nums2 + for (int num1 : nums1) { + int complement = tot - num1; // What we need from nums2 + + // If complement exists in nums2, add its frequency to our count + if (freq.find(complement) != freq.end()) { + pairCount += freq[complement]; + } + } + + return pairCount; + } +}; + +/** + * 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); + */