Problem · Array

Dynamic Pair Sum Queries

Learn this problem
MediumBoston Consulting Group logoBoston Consulting GroupNEW GRADINTERNOA

Problem statement

You are given two integer arrays a and b, and an array queries whose elements must be processed in order.

Each query has one of two forms:

  • [0, i, x]: assign x to a[i].
  • [1, x]: count the pairs of indices (i, j) such that a[i] + b[j] = x.

Return an array containing the counts produced by the type 1 queries, in query order.

Function

solution(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]

Initially, two pairs sum to 5: a[0] + b[1] = 3 + 2 and a[1] + b[0] = 4 + 1.

The update [0, 0, 1] assigns 1 to a[0], so a becomes [1, 4].

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

More Boston Consulting Group 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]
checking account