
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum value in an array after m range increment operations in C++
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,
#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,
#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
- Related Articles
- Print modified array after multiple array range increment operations in C Program.
- Binary array after M range toggle operations?
- Maximum count of equal numbers in an array after performing given operations in C++
- Maximum Possible Product in Array after performing given Operations in C++
- Program to find minimum possible maximum value after k operations in python
- Increment value of an array element with array object in MongoDB
- Increment a property value of an element in array object with MongoDB
- How to Auto-Increment Cell Value in Excel after Each Printing?
- Maximum subarray sum in an array created after repeated concatenation in C++
- Count of suffix increment/decrement operations to construct a given array in C++
- MongoDB Increment value inside nested array?
- Maximum subarray sum in an array created after repeated concatenation in C++ Program
- Increment MongoDB value inside a nested array
- Operations on an Array in Data Structures
- Print pair with maximum AND value in an array in C Program.
