Queries to update array by adding or multiplying array elements and print the element present at specified index | Function to modify the array by performing given queries ; Stores the multiplication of all letegers 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 ] ; Prlet the result for the current query ; Driver Code
function Query ( arr , N , Q ) { let mul = 1 ; let add = 0 ; for ( let i = 0 ; i < Q . length ; 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 { let ans = arr [ Q [ i ] [ 1 ] ] * mul + add ; document . write ( ans + " " ) ; } } } let arr = [ 3 , 1 , 23 , 45 , 100 ] ; let N = arr . length ; let Q = [ [ 1 , 2 ] , [ 0 , 10 ] , [ 2 , 3 ] , [ 1 , 5 ] , [ 2 , 4 ] ] ; Query ( arr , N , Q ) ;