Maximum element present in the array after performing queries to add K to range of indices [ L , R ] | Function to find the max sum after processing q queries ; Store the cumulative sum ; Store the maximum sum ; Iterate over the range 0 to q ; Variables to extract values from vector ; Iterate over the range [ 1 , n ] ; Calculate cumulative sum ; Calculate maximum sum ; Return the maximum sum after q queries ; Stores the size of array and number of queries ; Stores the sum ; Storing input queries ; Function call to find the maximum sum
function max_sum ( a , v , q , n ) { let x = 0 ; let m = Number . MIN_SAFE_INTEGER ; for ( let i = 0 ; i < q ; i ++ ) { let p , q , k ; p = v [ i ] [ 0 ] [ 0 ] ; q = v [ i ] [ 0 ] [ 1 ] ; k = v [ i ] [ 1 ] ; a [ p ] += k ; if ( q + 1 <= n ) a [ q + 1 ] -= k ; } for ( let i = 1 ; i <= n ; i ++ ) { x += a [ i ] ; m = Math . max ( m , x ) ; } return m ; } let n = 10 , q = 3 ; let a = new Array ( n + 5 ) . fill ( 0 ) ; let v = new Array ( q ) . fill ( 0 ) . map ( ( ) => new Array ( 2 ) . fill ( 0 ) . map ( ( ) => new Array ( 2 ) . fill ( 0 ) ) ) ; v [ 0 ] [ 0 ] [ 0 ] = 1 ; v [ 0 ] [ 0 ] [ 1 ] = 5 ; v [ 0 ] [ 1 ] = 3 ; v [ 1 ] [ 0 ] [ 0 ] = 4 ; v [ 1 ] [ 0 ] [ 1 ] = 8 ; v [ 1 ] [ 1 ] = 7 ; v [ 2 ] [ 0 ] [ 0 ] = 6 ; v [ 2 ] [ 0 ] [ 1 ] = 9 ; v [ 2 ] [ 1 ] = 1 ; document . write ( max_sum ( a , v , q , n ) ) ;