java.lang.Object
g1801_1900.s1865_finding_pairs_with_a_certain_sum.FindSumPairs

public class FindSumPairs extends Object
1865 - Finding Pairs With a Certain Sum.<p>Medium</p> <p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>. You are tasked to implement a data structure that supports queries of two types:</p> <ol> <li><strong>Add</strong> a positive integer to an element of a given index in the array <code>nums2</code>.</li> <li><strong>Count</strong> the number of pairs <code>(i, j)</code> such that <code>nums1[i] + nums2[j]</code> equals a given value (<code>0 <= i < nums1.length</code> and <code>0 <= j < nums2.length</code>).</li> </ol> <p>Implement the <code>FindSumPairs</code> class:</p> <ul> <li><code>FindSumPairs(int[] nums1, int[] nums2)</code> Initializes the <code>FindSumPairs</code> object with two integer arrays <code>nums1</code> and <code>nums2</code>.</li> <li><code>void add(int index, int val)</code> Adds <code>val</code> to <code>nums2[index]</code>, i.e., apply <code>nums2[index] += val</code>.</li> <li><code>int count(int tot)</code> Returns the number of pairs <code>(i, j)</code> such that <code>nums1[i] + nums2[j] == tot</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong> [&ldquo;FindSumPairs&rdquo;, &ldquo;count&rdquo;, &ldquo;add&rdquo;, &ldquo;count&rdquo;, &ldquo;count&rdquo;, &ldquo;add&rdquo;, &ldquo;add&rdquo;, &ldquo;count&rdquo;] [[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]</p> <p><strong>Output:</strong> [null, 8, null, 2, 1, null, null, 11]</p> <p><strong>Explanation:</strong></p> <p>FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);</p> <p>findSumPairs.count(7); // return 8; pairs (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) make 2 + 5 and pairs (5,1), (5,5) make 3 + 4</p> <p>findSumPairs.add(3, 2); // now nums2 = [1,4,5, <strong>4</strong><code>,5,4</code>]</p> <p>findSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5</p> <p>findSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1</p> <p>findSumPairs.add(0, 1); // now nums2 = [**<code>2</code> ** ,4,5,4<code>,5,4</code>]</p> <p>findSumPairs.add(1, 1); // now nums2 = [<code>2</code>, <strong>5</strong> ,5,4<code>,5,4</code>]</p> <p>findSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) make 2 + 5 and pairs (5,3), (5,5) make 3 + 4</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums1.length <= 1000</code></li> <li><code>1 <= nums2.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums1[i] <= 10<sup>9</sup></code></li> <li><code>1 <= nums2[i] <= 10<sup>5</sup></code></li> <li><code>0 <= index < nums2.length</code></li> <li><code>1 <= val <= 10<sup>5</sup></code></li> <li><code>1 <= tot <= 10<sup>9</sup></code></li> <li>At most <code>1000</code> calls are made to <code>add</code> and <code>count</code> <strong>each</strong>.</li> </ul>
  • Constructor Details

    • FindSumPairs

      public FindSumPairs(int[] nums1, int[] nums2)
  • Method Details

    • add

      public void add(int index, int val)
    • count

      public int count(int tot)