Problem · Array

Find Sum Pairs

MediumTiktokOA
See Tiktok hiring insights

You are given two arrays of integers, 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 array containing the results of the queries of the form [1, x], in the same order as those queries appear.

Examples
01 · 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], there are two valid pairs: a[0] + b[1] = 3 + 2 = 5 and a[1] + b[0] = 4 + 1 = 5.

The query [0, 0, 1] changes a[0] to 1, so a = [1, 4].

For the final query [1, 5], only a[1] + b[0] = 4 + 1 = 5 remains valid.

02 · Example 2
a = [1, 1]
b = [2, 3]
queries = [[1, 3], [0, 1, 2], [1, 3], [1, 4]]
return = [2, 1, 2]

Initially both values in a pair with b[0] = 2 to form 3, so the first answer is 2.

After assigning a[1] = 2, the arrays are a = [1, 2] and b = [2, 3]. There is one pair that sums to 3 and two pairs that sum to 4.

Constraints
  • a and b are arrays of integers.
  • Each update query has the form [0, i, x].
  • Each count query has the form [1, x].
More Tiktok problems
drafts saved locally
public int[] solution(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]
sign in to submit