Skip to content

1865. Finding Pairs With a Certain Sum #1894

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to efficiently handle two operations on two given arrays: adding a value to an element in the second array and counting the number of pairs from both arrays that sum to a given value. The challenge lies in optimizing these operations, especially the count operation, given the constraints.

Approach

  1. Initialization:

    • Store the given arrays nums1 and nums2.
    • Create a frequency map (hash table) for nums2 to keep track of how many times each number appears. This allows efficient updates and lookups.
  2. Add Operation:

    • When adding a value val to an element at a specific index in nums2, first retrieve the old value at that index.
    • Compute the new value by adding val to the old value.
    • Upda…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jul 6, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jul 6, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants