Maximum element present in the array after performing queries to add K to range of indices [ L , R ] | Java program for the above approach ; 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 ; Driver code ; Stores the size of array and number of queries ; Stores the sum ; Storing input queries ; Function call to find the maximum sum
import java . lang . * ; import java . util . * ; class GFG { static int max_sum ( int a [ ] , ArrayList < ArrayList < Integer > > v , int q , int n ) { int x = 0 ; int m = Integer . MIN_VALUE ; for ( int i = 0 ; i < q ; i ++ ) { int p , qq , k ; p = v . get ( i ) . get ( 0 ) ; qq = v . get ( i ) . get ( 1 ) ; k = v . get ( i ) . get ( 2 ) ; a [ p ] += k ; if ( qq + 1 <= n ) a [ qq + 1 ] -= k ; } for ( int i = 1 ; i <= n ; i ++ ) { x += a [ i ] ; m = Math . max ( m , x ) ; } return m ; } public static void main ( String [ ] args ) { int n = 10 , q = 3 ; int [ ] a = new int [ n + 5 ] ; ArrayList < ArrayList < Integer > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < q ; i ++ ) v . add ( new ArrayList < > ( ) ) ; v . get ( 0 ) . add ( 1 ) ; v . get ( 0 ) . add ( 5 ) ; v . get ( 0 ) . add ( 3 ) ; v . get ( 1 ) . add ( 4 ) ; v . get ( 1 ) . add ( 8 ) ; v . get ( 1 ) . add ( 7 ) ; v . get ( 2 ) . add ( 6 ) ; v . get ( 2 ) . add ( 9 ) ; v . get ( 2 ) . add ( 1 ) ; System . out . println ( max_sum ( a , v , q , n ) ) ; } }