Finding Pairs With a Certain Sum - Problem
Design a Dynamic Pair Sum Finder

You're tasked with building a smart data structure that can efficiently handle two types of operations on a pair of integer arrays:

๐Ÿ“Š Array Management: You have two integer arrays nums1 and nums2. While nums1 remains static, nums2 can be modified by adding values to specific indices.

๐Ÿ” Pair Counting: For any given target sum, you need to quickly count how many pairs (i, j) exist such that nums1[i] + nums2[j] = target.

The Challenge: Since nums2 can be frequently updated, your solution must efficiently handle both modifications and queries without recalculating everything from scratch each time.

Operations to implement:
โ€ข FindSumPairs(nums1, nums2) - Initialize with the two arrays
โ€ข add(index, val) - Add val to nums2[index]
โ€ข count(tot) - Count pairs that sum to tot

Input & Output

basic_operations.py โ€” Python
$ Input: nums1 = [1, 1, 2, 2, 2, 3], nums2 = [1, 4, 5, 2, 5, 4] operations: count(7), add(3, 2), count(8), count(4), add(0, 1), add(1, 1), count(7)
โ€บ Output: [null, 8, null, 2, 1, null, null, 11]
๐Ÿ’ก Note: Initial: count(7) finds 8 pairs. After add(3,2), nums2=[1,4,5,4,5,4]. count(8) finds 2 pairs, count(4) finds 1 pair. After more adds, count(7) finds 11 pairs.
edge_case_empty.py โ€” Python
$ Input: nums1 = [1], nums2 = [1] operations: count(2), add(0, 1), count(3)
โ€บ Output: [null, 1, null, 1]
๐Ÿ’ก Note: Single element arrays. Initially nums1[0] + nums2[0] = 1+1 = 2, so count(2)=1. After add(0,1), nums2=[2], so nums1[0] + nums2[0] = 1+2 = 3, count(3)=1.
no_pairs_found.py โ€” Python
$ Input: nums1 = [1, 1], nums2 = [1, 1] operations: count(5), add(0, 3), count(5)
โ€บ Output: [null, 0, null, 2]
๐Ÿ’ก Note: Initially max possible sum is 1+1=2, so count(5)=0. After add(0,3), nums2=[4,1], now we have sums 1+4=5 and 1+4=5, so count(5)=2.

Visualization

Tap to expand
๐Ÿฝ๏ธ Restaurant Combo System๐Ÿ“‹ Fixed Appetizer Menu (nums1)๐Ÿฅ— Salad$3๐Ÿค Shrimp$5๐Ÿง€ Cheese$4๐Ÿ”„ Dynamic Main Course (nums2)๐Ÿ” Burger$7๐Ÿ Pasta$8๐ŸŸ Fish$6๐Ÿ“Š Frequency Chart (Hash Map)$6 (Fish): 1 portion$7 (Burger): 1 portion$8 (Pasta): 1 portion๐Ÿ’ฐ Customer Query: $12 Budget๐Ÿฅ— Salad ($3) + ? = $12 โ†’ Need $9 mainโŒ No $9 main course available๐Ÿค Shrimp ($5) + ? = $12 โ†’ Need $7 mainโœ… Found $7 Burger: 1 combo๐Ÿง€ Cheese ($4) + ? = $12 โ†’ Need $8 mainโœ… Found $8 Pasta: 1 comboTotal: 2 combos for $12๐Ÿ“ˆ Price Update: Fish $6 โ†’ $91. Remove one $6 from frequency chart2. Add one $9 to frequency chart3. Next queries use updated chart instantly!๐Ÿ“Š Updated Frequency Chart$7 (Burger): 1 portion$8 (Pasta): 1 portion$9 (Fish): 1 portion๐Ÿš€ O(n) query time by leveraging hash map lookups!
Understanding the Visualization
1
Setup Menu System
Store fixed appetizer prices and create a frequency chart of main course prices
2
Customer Budget Query
For each appetizer price, quickly lookup how many main courses fit the remaining budget
3
Price Update
When main course prices change, efficiently update the frequency chart
4
Instant Response
Serve customers instantly with updated combo counts using the optimized system
Key Takeaway
๐ŸŽฏ Key Insight: Use a frequency map for the changing array (nums2) while keeping the static array (nums1) simple. This allows O(1) updates and O(n) queries instead of O(nร—m) brute force.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n) per count query, O(1) per add

Count queries iterate through nums1 (size n) with O(1) hash lookups. Add operations just update hash map.

n
2n
โœ“ Linear Growth
Space Complexity
O(m)

Need to store frequency map of nums2 values, which has at most m unique values

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums1.length โ‰ค 1000
  • 1 โ‰ค nums2.length โ‰ค 105
  • 1 โ‰ค nums1[i] โ‰ค 109
  • 1 โ‰ค nums2[i] โ‰ค 106
  • At most 1000 calls will be made to add and count
Asked in
Google 45 Amazon 38 Microsoft 28 Meta 22
21.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen