Problem · Array

Find Sum Pairs (for mle also :)

Learn this problem
MediumByteDance logoByteDanceINTERNNEW GRADOA

Problem statement

You are given two integer arrays, a and b, and an array queries. Process every query in order.

Each query has one of the following forms:

  • [0, i, x]: assign a[i] the value x.
  • [1, x]: count the number of pairs of indices i and j such that a[i] + b[j] = x.

Return an integer array containing the results of the [1, x] queries in the order they appear.

Function

findSumPairs(a: int[], b: int[], queries: int[][]) → int[]

Examples

Example 1

a = [3,4]b = [1,2,3]queries = [[1,5],[0,0,1],[1,5]]return = [2,1]

For the first query [1,5], the pairs are a[0] + b[1] = 3 + 2 and a[1] + b[0] = 4 + 1, so the result is 2.

The query [0,0,1] changes a to [1,4]. For the final query, only a[1] + b[0] = 4 + 1 sums to 5, so the result is 1.

Example 2

a = [2,3]b = [1,2,2]queries = [[1,4],[0,0,3],[1,5]]return = [3,4]

Initially, a[0] = 2 pairs with both occurrences of 2 in b, and a[1] = 3 pairs with b[0] = 1, giving 3 pairs that sum to 4.

After assigning a[0] = 3, each of the two values in a pairs with each of the two occurrences of 2 in b, giving 4 pairs that sum to 5.

Constraints

  • 1 <= a.length
  • 1 <= b.length
  • 1 <= queries.length
  • Every query is either [0, i, x] or [1, x].
  • Every update index i is valid for a.

More ByteDance problems

drafts saved locally
public int[] findSumPairs(int[] a, int[] b, int[][] queries) {
    // write your code here
}
a[3,4]
b[1,2,3]
queries[[1,5],[0,0,1],[1,5]]
expected[2,1]
checking account