Find the sum of array after performing every query
Last Updated :
19 May, 2021
Given an array arr[] of size N and Q queries where every query contains two integers X and Y, the task is to find the sum of an array after performing each Q queries such that for every query, the element in the array arr[] with value X is updated to Y. Find the sum of the array after every query.
Examples:
Input: arr[] ={1, 2, 3, 4}, Q = {(1, 2), (3, 4), (2, 4)}
Output: 11 12 16
Explanation:
1st operation is to replace each 1 with 2
So array becomes ar[ ] ={2, 2, 3, 4} ans sum = 11
2nd operation is to replace each 3 with 4
So array becomes ar[ ] ={2, 2, 4, 4} ans sum = 12
3rd operation is to replace each 2 with 4
So array becomes ar[ ] ={4, 4, 4, 4} ans sum = 16
Input: arr[] = {1}, Q = {(1, 2)}
Output: 2
Naive Approach: The naive approach is to traverse every query and for each query update each element in the array arr[] with value X to Y by traversing the array. Print the sum of all elements in arr[] after each query is performed.
Time Complexity: O(N*Q)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to compute the sum of all the element in the array(say sum) arr[] and store the frequency of all elements in a unordered_map(Say M). For each query (X, Y) do the following:
- Find the frequency of X from unordered_map M.
- Decrease sum by X*M[X], for excluding the sum of X.
- Increase sum by Y*M[X], for excluding the sum of Y.
- Increase the frequency of Y in the map by M[X].
- Finally, print the sum and remove X from the map M.
Below is the implementation of the above approach
C++14
#include <bits/stdc++.h>
using namespace std;
void solve( int ar[], int n, int b[],
int c[], int q)
{
unordered_map< int , int > mp;
int sum = 0;
for ( int x = 0; x < n; x++) {
sum += ar[x];
mp[ar[x]]++;
}
for ( int x = 0; x < q; x++) {
int occur1 = mp[b[x]];
sum = sum - occur1 * b[x];
mp.erase(b[x]);
sum = sum + occur1 * c[x];
mp] += occur1;
cout << sum << " " ;
}
}
int main()
{
int ar[] = { 1, 2, 3, 4 };
int n = 4;
int q = 3;
int b[] = { 1, 3, 2 };
int c[] = { 2, 4, 4 };
solve(ar, n, b, c, q);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void solve( int ar[], int n, int b[],
int c[], int q)
{
Map<Integer, Integer> mp = new HashMap<>();
int sum = 0 ;
for ( int x = 0 ; x < n; x++)
{
sum += ar[x];
mp.put(ar[x], mp.getOrDefault(ar[x], 0 ) + 1 );
}
for ( int x = 0 ; x < q; x++)
{
int occur1 = mp.get(b[x]);
sum = sum - occur1 * b[x];
mp.remove(b[x]);
sum = sum + occur1 * c[x];
mp.put(c[x], mp.get(c[x]) + occur1);
System.out.print(sum + " " );
}
}
public static void main (String[] args)
{
int ar[] = { 1 , 2 , 3 , 4 };
int n = 4 ;
int q = 3 ;
int b[] = { 1 , 3 , 2 };
int c[] = { 2 , 4 , 4 };
solve(ar, n, b, c, q);
}
}
|
Python3
def solve(ar, n, b, c, q):
mp = {}
sum = 0
for x in range (n):
sum + = ar[x]
mp[ar[x]] = mp.get(ar[x], 0 ) + 1
for x in range (q):
occur1 = mp[b[x]]
sum = sum - occur1 * b[x]
del mp[b[x]]
sum = sum + occur1 * c[x]
mp] + = occur1
print ( sum , end = " " )
if __name__ = = '__main__' :
ar = [ 1 , 2 , 3 , 4 ]
n = 4
q = 3
b = [ 1 , 3 , 2 ]
c = [ 2 , 4 , 4 ]
solve(ar, n, b, c, q)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void solve( int []ar, int n,
int []b, int []c,
int q)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
int sum = 0;
for ( int x = 0; x < n; x++)
{
sum += ar[x];
if (mp.ContainsKey(ar[x]))
mp[ar[x]] = mp[ar[x]] + 1;
else
mp.Add(ar[x], 1);
}
for ( int x = 0; x < q; x++)
{
int occur1 = mp[b[x]];
sum = sum - occur1 * b[x];
mp.Remove(b[x]);
sum = sum + occur1 * c[x];
if (mp.ContainsKey(c[x]))
mp] = mp] + occur1;
Console.Write(sum + " " );
}
}
public static void Main(String[] args)
{
int []ar = {1, 2, 3, 4};
int n = 4;
int q = 3;
int []b = {1, 3, 2};
int []c = {2, 4, 4};
solve(ar, n, b, c, q);
}
}
|
Javascript
<script>
function solve(ar, n, b, c, q)
{
var mp = new Map();
var sum = 0;
for ( var x = 0; x < n; x++) {
sum += ar[x];
if (mp.has(ar[x]))
mp.set(ar[x], mp.get(ar[x])+1)
else
mp.set(ar[x], 1);
}
for ( var x = 0; x < q; x++) {
var occur1 = mp.get(b[x]);
sum = sum - occur1 * b[x];
mp.set(b[x], 0);
sum = sum + occur1 * c[x];
if (mp.has(c[x]))
mp.set(c[x], mp.get(c[x])+occur1)
else
mp.set(c[x], occur1);
document.write( sum + " " );
}
}
var ar = [1, 2, 3, 4];
var n = 4;
var q = 3;
var b = [1, 3, 2];
var c = [2, 4, 4];
solve(ar, n, b, c, q);
</script>
|
Time Complexity: O(N + Q)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...