FastPrepFastPrep
Problem Brief

Sum After Query Update

OA

Hello! I feel like it's a duplicate of another existing problem..but I couldnt find it. If you happen to know the duplicate, please let me know. Manyyyy thanks in advance. You are the best!

There are data points as integer array and array of queries. Where each query is: q[i][0] = old value, q[i][1] = new value. After processing i-th query you need to update all data points having value 'old value' to 'new value' and calculate total sum of all data points.

Return array of sums after each query executed.

Watch out for special cases like duplicates and zero updates (same value or value doesn't exist in the current array).

Function Description

Complete the function getSumAfterQueries in the editor.

getSumAfterQueries has the following parameters:

  1. 1. int[] data: an array of integers representing the data points
  2. 2. int[][] queries: an array of queries where queries[i][0] is the old value and queries[i][1] is the new value

Returns

int[]: an array of integers representing the sum of data points after each query

1Example 1

Input
data = [2,3,4,5,6], queries = [[2,3],[2,4],[4,10]]
Output
[21,21,27]
Explanation
After each update:
  • [2,3] => [3,3,4,5,6] - 21
  • [2,4] => [3,3,4,5,6] - 21
  • [4,10] => [3,3,10,5,6] - 27
public int[] getSumAfterQueries(int[] data, int[][] queries) {
  // write your code here
}
Input

data

[2,3,4,5,6]

queries

[[2,3],[2,4],[4,10]]

Output

[21,21,27]

Sign in to submit your solution.