Advertisement

Finding Pairs With a Certain Sum - LeetCode 1865 Solution

Finding Pairs With a Certain Sum - Complete Solution Guide

Finding Pairs With a Certain Sum is LeetCode problem 1865, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two integer arrays nums1 and nums2 . You are tasked to implement a data structure that supports queries of two types: Add a positive integer to an element of a given index in the array nums2 . Count the number of pairs (i, j) such that nums1[i] + nums2[j] equals a given value ( 0 <= i < nums1.length and 0 <= j < nums2.length ). Implement the FindSumPairs class: FindSumPairs(int[] nums1, int[] nums2) Initializes the FindSumPairs object with two integer arrays nums1 and nums2 . void

Detailed Explanation

The problem requires implementing a `FindSumPairs` class that can handle two types of queries: `add` and `count`. The class is initialized with two integer arrays, `nums1` and `nums2`. The `add(index, val)` operation increases the value of `nums2[index]` by `val`. The `count(tot)` operation returns the number of pairs (i, j) such that `nums1[i] + nums2[j] == tot`. In essence, we need an efficient way to update elements in `nums2` and quickly determine the number of pairs from `nums1` and `nums2` that sum up to a given target value `tot`.

Solution Approach

The provided solution uses hash maps (or Counters in Python) to store the frequencies of elements in `nums1` and `nums2`. During initialization, the frequencies of each element in both arrays are calculated and stored. The `add` operation updates the value at the specified index in `nums2`, and the corresponding frequency in the `freq2` hash map is adjusted. The `count` operation iterates through the frequency map of `nums1`. For each number in `nums1`, it calculates the required complement in `nums2` to reach the target sum `tot`. It then multiplies the frequency of that number in `nums1` by the frequency of its complement in `nums2` and adds the result to the total count.

Step-by-Step Algorithm

  1. Step 1: Initialize the `FindSumPairs` class with `nums1` and `nums2`. Create frequency maps `freq1` and `freq2` to store the count of each element in `nums1` and `nums2`, respectively.
  2. Step 2: Implement the `add(index, val)` method. Decrement the frequency of the old value at `nums2[index]` in `freq2`. If the frequency becomes 0, remove the element from `freq2`. Update the value at `nums2[index]` by adding `val`. Increment the frequency of the new value at `nums2[index]` in `freq2`.
  3. Step 3: Implement the `count(tot)` method. Iterate through the `freq1` map. For each element `num` in `nums1` and its corresponding frequency `freq`, calculate the target `target = tot - num`. Check if the `target` exists in `freq2`. If it does, multiply `freq` with the frequency of `target` in `freq2` and add it to the running `result`. Return the `result`.

Key Insights

  • Insight 1: Storing frequencies of elements in `nums1` and `nums2` using hash maps allows for efficient counting of pairs that sum to the target.
  • Insight 2: Using a hash map (or counter in Python) for frequencies enables O(1) lookups when counting pairs and updating frequencies after an `add` operation.
  • Insight 3: The `add` operation only modifies `nums2`, so the frequencies of `nums1` remain constant. Only the frequencies of `nums2` need to be updated.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Design.

Companies

Asked at: Databricks, Quora.