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

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] of N elements initialized with 0. Our task is to create a program to find the Maximum value in an array after m range increment operations in C++.

Problem Description

On the array, we will perform m range increment operation of the type,

update[L, R, K] = Add value K, to all elements in the range.

After performing m operations on the array. We need to find the element with maximum value in the array.

Let’s take an example to understand the problem,

Input

N = 6, m = 4
Update[][] = {{1, 4, 12}, {0, 3, 5}, {1, 5, 7}, {3, 5, 10}}

Output

34

Explanation

arr[] = {0, 0, 0, 0, 0, 0}
Update 1 {1, 4, 12} : arr[] = {0, 12, 12, 12, 12, 0}
Update 2 {0, 3, 5} : arr[] = {5, 17, 17, 17, 12, 0}
Update 3 {1, 5, 7} : arr[] = {5, 24, 24, 24, 19, 7}
Update 4 {3, 5, 10} : arr[] = {5, 24, 24, 34, 29, 17}

Solution Approach

A simple method to solve the problem is by simply update the values of the array and then after all operations are done. Find the maximum element of the array.

Example

Program to illustrate the working of our solution,

 Live Demo

#include<iostream>
using namespace std;

int findmax(int arr[], int N){
   int maxVal = 0;
   for(int i = 0; i < N; i++){
      if(maxVal < arr[i]){
         maxVal = arr[i];
      }
   }
   return maxVal;
}
void updateVal(int arr[], int L, int R, int K){
   for(int i = L; i <= R; i++ ){
      arr[i] += K;
   }
}
int main(){
   int N = 5;
   int arr[N] = {0};
   int M = 4;
   int rangeIncOperation[M][3] = {{1, 4, 12}, {0, 3, 5}, {1, 5, 7}, {3, 5, 10}};
   for(int i = 0; i < M; i++){
      updateVal(arr, rangeIncOperation[i][0], rangeIncOperation[i][1], rangeIncOperation[i][2]);
   }
   cout<<"The maximum value in the array after "<<M<<" range increment operations is "<<findmax(arr, N);
   return 0;
}

Output

The maximum value in the array after 4 range increment operations is 34

This operation is good but iterates over the range for each query making the complexity of the order O(m*N).

A better approach is to add K to L and subtract K from R+1 for each range increment operation. And then find the greatest maxima value i.e. update sum value for each value in the array and find the maximum value that occurred in the operation.

Example

Program to illustrate the working of our solution,

 Live Demo

#include<iostream>
using namespace std;

int FindMaximum(int a, int b){
   if(a > b)
      return a;
      return b;
   }
int findmax(int arr[], int N){
   int maxVal = 0;
   int sum = 0;
   for(int i = 0; i < N; i++){
      sum += arr[i];
      maxVal = FindMaximum(maxVal, sum);
   }
   return maxVal;
}
void updateVal(int arr[], int L, int R, int K){
   arr[L] += K;
   arr[R+1] -= K;
}
int main(){
   int N = 5;
   int arr[N] = {0};
   int M = 4;
   int rangeIncOperation[M][3] = {{1, 4, 12}, {0, 3, 5}, {1, 5, 7}, {3, 5, 10}};
   for(int i = 0; i < M; i++){
      updateVal(arr, rangeIncOperation[i][0], rangeIncOperation[i][1], rangeIncOperation[i][2]);
   }
   cout<<"The maximum value in the array after "<<M<<" range increment operations is "<<findmax(arr, N);
   return 0;
}

Output

The maximum value in the array after 4 range increment operations is 34
raja
Published on 27-Jul-2020 08:34:40
Advertisements