Consider an array of size n with all initial values as 0. We need to perform the following m range increment operations.
increment(a, b, k) : Increment values from 'a'
to 'b' by 'k'.
After m operations, we need to calculate the maximum of the values in the array.
Examples:
Input : n = 5 m = 3
a = 0, b = 1, k = 100
a = 1, b = 4, k = 100
a = 2, b = 3, k = 100
Output : 200
Explanation:
Initially array = {0, 0, 0, 0, 0}
After first operation:
array = {100, 100, 0, 0, 0}
After second operation:
array = {100, 200, 100, 100, 100}
After third operation:
array = {100, 200, 200, 200, 100}
Maximum element after m operations is 200.
Input : n = 4 m = 3
a = 1, b = 2, k = 603
a = 0, b = 0, k = 286
a = 3, b = 3, k = 882
Output : 882
Explanation:
Initially array = {0, 0, 0, 0}
After first operation:
array = {0, 603, 603, 0}
After second operation:
array = {286, 603, 603, 0}
After third operation:
array = {286, 603, 603, 882}
Maximum element after m operations is 882.
A naive method is to perform each operation on the given range and then, at last, find the maximum number.
C++
#include<bits/stdc++.h>
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;
}
|
Java
import java.util.*;
class GFG{
static int findMax( int n, int a[],
int b[], int k[], int m)
{
int [] arr = new int [n];
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 = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
res = Math.max(res, arr[i]);
return res;
}
public static void main (String[] args)
{
int n = 5 ;
int a[] = { 0 , 1 , 2 };
int b[] = { 1 , 4 , 3 };
int k[] = { 100 , 100 , 100 };
int m = a.length;
System.out.println( "Maximum value after 'm' " +
"operations is " +
findMax(n, a, b, k, m));
}
}
|
Python3
import sys
def findMax(n, a, b, k, m):
arr = [ 0 ] * n
for i in range (m):
lowerbound = a[i]
upperbound = b[i]
for j in range (lowerbound,
upperbound + 1 ):
arr[j] + = k[i]
res = - sys.maxsize - 1
for i in range (n):
res = max (res, arr[i])
return res
if __name__ = = "__main__" :
n = 5
a = [ 0 , 1 , 2 ]
b = [ 1 , 4 , 3 ]
k = [ 100 , 100 , 100 ]
m = len (a)
print ( "Maximum value after 'm' operations is " ,
findMax(n, a, b, k, m))
|
C#
using System;
public class GFG
{
static int findMax( int n, int [] a,
int [] b, int [] k, int m)
{
int [] arr = new int [n];
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 = Int32.MinValue;
for ( int i = 0; i < n; i++)
res = Math.Max(res, arr[i]);
return res;
}
static public void Main ()
{
int n = 5;
int [] a = { 0, 1, 2 };
int [] b = { 1, 4, 3 };
int [] k = { 100, 100, 100 };
int m = a.Length;
Console.WriteLine( "Maximum value after 'm' " +
"operations is " +
findMax(n, a, b, k, m));
}
}
|
Javascript
<script>
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( "Maximum value after 'm' " +
"operations is " +
findMax(n, a, b, k, m));
</script>
|
OutputMaximum value after 'm' operations is 200
Time Complexity: O(m * max(range)). Here max(range) means maximum elements to which k is added in a single operation.
Auxiliary space: O(n)
Efficient method: The idea is similar to this post.
Perform two things in a single operation:
- Add k-value to the only lower_bound of a range.
- Reduce the upper_bound + 1 index by a k-value.
After all operations, add all values, check the maximum sum, and print the maximum sum.
C++
#include<bits/stdc++.h>
using namespace std;
int findMax( int n, int m, int a[], int b[], int k[])
{
int arr[n+1];
memset (arr, 0, sizeof (arr));
for ( int i=0; i<m; i++)
{
int lowerbound = a[i];
int upperbound = b[i];
arr[lowerbound] += k[i];
arr[upperbound+1] -= k[i];
}
long long sum = 0, res = INT_MIN;
for ( int i=0; i < n; ++i)
{
sum += arr[i];
res = max(res, sum);
}
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, m, a, b, k);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long findMax( int n, int m,
int a[], int b[],
int k[])
{
int []arr = new int [n + 1 ];
for ( int i = 0 ; i < m; i++)
{
int lowerbound = a[i];
int upperbound = b[i];
arr[lowerbound] += k[i];
arr[upperbound + 1 ] -= k[i];
}
long sum = 0 , res = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; ++i)
{
sum += arr[i];
res = Math.max(res, sum);
}
return res;
}
public static void main (String[] args)
{
int n = 5 ;
int a[] = { 0 , 1 , 2 };
int b[] = { 1 , 4 , 3 };
int k[] = { 100 , 100 , 100 };
int m = a.length;
System.out.println( "Maximum value after " +
"'m' operations is " +
findMax(n, m, a, b, k));
}
}
|
Python3
import sys
def findMax(n, m, a, b, k):
arr = [ 0 for i in range (n + 1 )]
for i in range (m):
lowerbound = a[i]
upperbound = b[i]
arr[lowerbound] + = k[i]
arr[upperbound + 1 ] - = k[i]
sum = 0
res = - 1 - sys.maxsize
for i in range (n):
sum + = arr[i]
res = max (res, sum )
return res
n = 5
a = [ 0 , 1 , 2 ]
b = [ 1 , 4 , 3 ]
k = [ 100 , 100 , 100 ]
m = len (a)
print ( "Maximum value after" , "'m' operations is" , findMax(n, m, a, b, k))
|
C#
using System.Collections.Generic;
using System;
class GFG{
static long findMax( int n, int m,
int []a, int []b,
int []k)
{
int []arr = new int [n + 1];
for ( int i = 0; i < m; i++)
{
int lowerbound = a[i];
int upperbound = b[i];
arr[lowerbound] += k[i];
arr[upperbound + 1] -= k[i];
}
long sum = 0, res = -10000000;
for ( int i = 0; i < n; ++i)
{
sum += arr[i];
res = Math.Max(res, sum);
}
return res;
}
public static void Main ()
{
int n = 5;
int []a = {0, 1, 2};
int []b = {1, 4, 3};
int []k = {100, 100, 100};
int m = a.Length;
Console.WriteLine( "Maximum value after " +
"'m' operations is " +
findMax(n, m, a, b, k));
}
}
|
Javascript
<script>
function findMax(n, m, a, b, k)
{
let arr = new Array(n + 1);
arr.fill(0);
for (let i = 0; i < m; i++)
{
let lowerbound = a[i];
let upperbound = b[i];
arr[lowerbound] += k[i];
arr[upperbound + 1] -= k[i];
}
let sum = 0, res = -10000000;
for (let i = 0; i < n; ++i)
{
sum += arr[i];
res = Math.max(res, sum);
}
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( "Maximum value after " +
"'m' operations is " +
findMax(n, m, a, b, k));
</script>
|
OutputMaximum value after 'm' operations is 200
Time complexity: O(m + n)
Auxiliary space: O(n)
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.