FastPrepDynamic Pair Sum Queries

Dynamic Pair Sum Queries

Duolingo logoDuolingo● MediumNEW GRADOA
Learn

Problem statement

You are given two integer arrays a and b and a sequence of queries. Process the queries in order.

  • A query [0, i, x] adds x to a[i].
  • A query [1, x] asks for the number of index pairs (i, j) such that the current values satisfy a[i] + b[j] = x.

Return the answers to all type-1 queries in their original order.

Function

processPairSumQueries(a: int[], b: int[], queries: int[][]) → long[]

Examples

Example 1

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

Before the update, only a[1] + b[0] = 4 + 1 = 5, so the first answer is 1. The update changes a[0] from 1 to 3. Then both 3 + 2 and 4 + 1 equal 5, so the second answer is 2.

Constraints

  • a, b, and queries are non-empty.
  • Every type-0 query has the form [0, i, x] with 0 <= i < a.length.
  • Every type-1 query has the form [1, x].
  • Array b is never modified.

More Duolingo problems

See Duolingo hiring insights
public long[] processPairSumQueries(int[] a, int[] b, int[][] queries) {
    // write your code here
}
a[1,4]
b[1,2,3]
queries[[1,5],[0,0,2],[1,5]]
expected[1,2]
Checking account…