Maximum value in an array after m range increment operations | 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 ; Number of values ; Value of k to be added at each operation
function findMax ( n , a , b , k , m ) { let arr = new Array ( n ) ; arr . fill ( 0 ) ; for ( let i = 0 ; i < m ; i ++ ) { let lowerbound = a [ i ] ; let upperbound = b [ i ] ; for ( let j = lowerbound ; j <= upperbound ; j ++ ) arr [ j ] += k [ i ] ; } let res = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) res = Math . max ( res , arr [ i ] ) ; return res ; } let n = 5 ; let a = [ 0 , 1 , 2 ] ; let b = [ 1 , 4 , 3 ] ; let k = [ 100 , 100 , 100 ] ; let m = a . length ; document . write ( " " + " " + findMax ( n , a , b , k , m ) ) ;