Maximum value in an array after m range increment operations in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find maximum value in an array after m range increment operations.

For this we will be provided with an array consisting of zero values and input statements to be executed. Our task is to implement the given input statements on the given array and find the maximum element.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//finding maximum element after executing operations
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;
}

Output

Maximum value after 'm' operations is 200
raja
Published on 27-Jul-2020 12:04:40
Advertisements