Maximum value in an array after m range increment operations | Java implementation of simple approach to find maximum value after m range increments . ; Function to find the maximum element after m operations ; Start performing m operations ; Store lower and upper index i . e . range ; Add ' k [ i ] ' value at this operation to whole range ; Find maximum value after all operations and return ; Driver Code ; Number of values ; Value of k to be added at each operation
import java . util . * ; class GFG { static int findMax ( int n , int a [ ] , int b [ ] , int k [ ] , int m ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < m ; i ++ ) { int lowerbound = a [ i ] ; int upperbound = b [ i ] ; for ( int j = lowerbound ; j <= upperbound ; j ++ ) arr [ j ] += k [ i ] ; } int res = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) res = Math . max ( res , arr [ i ] ) ; return res ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 0 , 1 , 2 } ; int b [ ] = { 1 , 4 , 3 } ; int k [ ] = { 100 , 100 , 100 } ; int m = a . length ; System . out . println ( " Maximum ▁ value ▁ after ▁ ' m ' ▁ " + " operations ▁ is ▁ " + findMax ( n , a , b , k , m ) ) ; } }