Maximum value in an array after m range increment operations | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMax ( int n , int a [ ] , int b [ ] , int k [ ] , int m ) { int arr [ n ] ; memset ( arr , 0 , sizeof ( arr ) ) ; 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 = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) res = max ( res , arr [ i ] ) ; return res ; } int main ( ) { int n = 5 ; int a [ ] = { 0 , 1 , 2 } ; int b [ ] = { 1 , 4 , 3 } ; int k [ ] = { 100 , 100 , 100 } ; int m = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " Maximum ▁ value ▁ after ▁ ' m ' ▁ operations ▁ is ▁ " << findMax ( n , a , b , k , m ) ; return 0 ; }