Given an integer array **arr[]** and **Q** queries, the task is to find the sum of the array for each query of the following type:

- Each query contains 2 integers
**X**and**Y**, where all the occurrences of**X**in**arr[]**is to be replaced by**Y**. - After each query print the sum of the array.

**Examples:**

Input:arr[] = { 1, 2, 1, 3, 2}, X[] = { 2, 3, 5 }, Y[] = { 3, 1, 2 }

Output:11 5 5

Explanation:

After 1st Query, Replace 2 with 3, arr[] = { 1, 3, 1, 3, 3 }, Sum = 11.

After 2nd Query, Replace 3 with 1, arr[] = { 1, 1, 1, 1, 1 }, Sum = 5.

After 3rd Query, Replace 5 with 2, arr[] = { 1, 1, 1, 1, 1 }, Sum = 5.

Input:arr[] = { 12, 22, 11, 11, 2}, X[] = {2, 11, 22}, Y[] = {12, 222, 2}

Output:68 490 470

**Naive Approach:**

The simplest approach to solve the problem mentioned above is to traverse through the array and replace all the instances of **X** with **Y ** for each query and calculate the sum.

**Time Complexity:** O(N * Q)

**Efficient Approach: **

To optimize the above method follow, the steps given below:

- Precompute and store the sum of the array in a variable
**S**and store the frequencies of array elements in a Map**count**. - Then, do the following for each query:
- Find the frequency of X stored in the map.
- Subtract
**X * count[X]**from**S**. - Set
**count[Y] = count[X]**and then**count[X] = 0**. - Add
**Y * count[Y]**to**S**. - Print the updated value of
**S**.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the sum ` `// of the array for the given Q queries ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that print the sum of ` `// the array for Q queries ` `void` `sumOfTheArrayForQuery(` `int` `* A, ` `int` `N, ` ` ` `int` `* X, ` `int` `* Y, ` ` ` `int` `Q) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Stores the frequencies ` ` ` `// of array elements ` ` ` `unordered_map<` `int` `, ` `int` `> count; ` ` ` ` ` `// Calculate the sum of ` ` ` `// the initial array and ` ` ` `// store the frequency of ` ` ` `// each element in map ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `sum += A[i]; ` ` ` `count[A[i]]++; ` ` ` `} ` ` ` ` ` `// Iterate for all the queries ` ` ` ` ` `for` `(` `int` `i = 0; i < Q; i++) { ` ` ` `// Store query values ` ` ` `int` `x = X[i], y = Y[i]; ` ` ` ` ` `// Decrement the sum accordingly ` ` ` `sum -= count[X[i]] * X[i]; ` ` ` ` ` `// Increment the sum accordingly ` ` ` `sum += count[X[i]] * Y[i]; ` ` ` ` ` `// Set count of Y[i] ` ` ` `count[Y[i]] += count[X[i]]; ` ` ` ` ` `// Reset count of X[i] ` ` ` `count[X[i]] = 0; ` ` ` ` ` `// Print the sum ` ` ` `cout << sum << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 1, 3, 2 }; ` ` ` `int` `X[] = { 2, 3, 5 }; ` ` ` `int` `Y[] = { 3, 1, 2 }; ` ` ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `int` `Q = ` `sizeof` `(X) / ` `sizeof` `(X[0]); ` ` ` ` ` `// Function call ` ` ` `sumOfTheArrayForQuery(arr, N, X, Y, Q); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

11 5 5

**Time Complexity:** O(N), as each query has computational complexity of O(1).

**Auxiliary Space:** O(N)

## Recommended Posts:

- Queries to find the XOR of an Array after replacing all occurrences of X by Y
- Most frequent element in Array after replacing given index by K for Q queries
- Maximum possible GCD after replacing at most one element in the given array
- Check if a substring can be Palindromic by replacing K characters for Q queries
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Find the Initial Array from given array after range sum queries
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Maximum Subarray Sum possible by replacing an Array element by its Square
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Remove all occurrences of any element for maximum array sum
- Maximum sum after rearranging the array for K queries
- Array range queries over range queries
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Printing frequency of each character just after its consecutive occurrences
- Sum of all distances between occurrences of same characters in a given string
- Game of replacing array elements
- Replacing an element makes array elements consecutive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.