Queries to update array by adding or multiplying array elements and print the element present at specified index | C ++ program for the above approach ; Function to modify the array by performing given queries ; Stores the multiplication of all integers of type 1 ; Stores the value obtained after performing queries of type 1 & 2 ; Iterate over the queries ; Query of type 0 ; Update the value of add ; Query of type 1 ; Update the value of mul ; Update the value of add ; Otherwise ; Store the element at index Q [ i ] [ 1 ] ; Print the result for the current query ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Query ( int arr [ ] , int N , vector < vector < int > > Q ) { int mul = 1 ; int add = 0 ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { if ( Q [ i ] [ 0 ] == 0 ) { add = add + Q [ i ] [ 1 ] ; } else if ( Q [ i ] [ 0 ] == 1 ) { mul = mul * Q [ i ] [ 1 ] ; add = add * Q [ i ] [ 1 ] ; } else { int ans = arr [ Q [ i ] [ 1 ] ] * mul + add ; cout << ans << " ▁ " ; } } } int main ( ) { int arr [ ] = { 3 , 1 , 23 , 45 , 100 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < vector < int > > Q = { { 1 , 2 } , { 0 , 10 } , { 2 , 3 } , { 1 , 5 } , { 2 , 4 } } ; Query ( arr , N , Q ) ; return 0 ; }