Sum of product of all unordered pairs in given range with update queries | Function to calculate the Pairwise Product Sum in range from L to R ; Loop to iterate over all possible pairs from L to R ; Print answer ; Function to update the Array element at index P to X ; Update the value at Pth index in the array ; Function to solve Q queries ; If Query is of type 1 ; If Query is of type 2 ; Driver Code
function pairwiseProductSum ( a , l , r ) { let sum = 0 ; for ( let j = l - 1 ; j <= r - 1 ; j ++ ) { for ( let k = j + 1 ; k <= r - 1 ; k ++ ) { sum += a [ j ] * a [ k ] ; } } document . write ( sum + " " ) ; } function updateArray ( a , p , x ) { a [ p - 1 ] = x ; } function solveQueries ( a , n , Q , query ) { for ( let i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } let A = [ 5 , 7 , 2 , 3 , 1 ] ; let N = A . length ; let Q = 3 ; let query = [ [ 1 , 1 , 3 ] , [ 2 , 2 , 5 ] , [ 1 , 2 , 5 ] , ] ; solveQueries ( A , N , Q , query ) ;