Maximum element present in the array after performing queries to add K to range of indices [L, R]
Last Updated :
20 Jan, 2023
Given an array arr[] consisting of N integers, ( initially set to 0 ) and an array Q[], consisting of queries of the form {l, r, k}, the task for each query is to add K to the indices l to r(both inclusive). After performing all queries, return the maximum element present the array.
Example:
Input: N=10, q[] = {{1, 5, 3}, {4, 8, 7}, {6, 9, 1}}
Output: 10
Explanation:
Initially the array is ? [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Query1 {1, 5, 3} results in [3, 3, 3, 3, 3, 0, 0, 0, 0, 0]
Query2 {4, 8, 7} results in [3, 3, 3, 10, 10, 7, 7, 7, 0, 0]
Query2 {6, 9, 1} results in [3, 3, 3, 10, 10, 8, 8, 8, 1, 0]
Maximum value in the updated array = 10
Approach: Follow the steps below to solve the problem.
- Traverse over the vector of queries and for each query {l, r, k}
- Add k to a[l] and subtract k from a[r+1]
- Initialize variable x = 0 to store the running sum and m = INT_MIN to store the maximum value
- Traverse the array, add elements to x, and update m.
- Print the maximum value m
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max_sum( int a[],
vector<pair<pair< int , int >, int > > v,
int q, int n)
{
int x = 0;
int m = INT_MIN;
for ( int i = 0; i < q; i++) {
int p, q, k;
p = v[i].first.first;
q = v[i].first.second;
k = v[i].second;
a[p] += k;
if (q + 1 <= n)
a[q + 1] -= k;
}
for ( int i = 1; i <= n; i++)
{
x += a[i];
m = max(m, x);
}
return m;
}
int main()
{
int n = 10, q = 3;
int a[n + 5] = { 0 };
vector<pair<pair< int , int >, int > > v(q);
v[0].first.first = 1;
v[0].first.second = 5;
v[0].second = 3;
v[1].first.first = 4;
v[1].first.second = 8;
v[1].second = 7;
v[2].first.first = 6;
v[2].first.second = 9;
v[2].second = 1;
cout << max_sum(a, v, q, n);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static int max_sum( int a[],
ArrayList<ArrayList<Integer>> v,
int q, int n)
{
int x = 0 ;
int m = Integer.MIN_VALUE;
for ( int i = 0 ; i < q; i++)
{
int p, qq, k;
p = v.get(i).get( 0 );
qq = v.get(i).get( 1 );
k = v.get(i).get( 2 );
a[p] += k;
if (qq + 1 <= n)
a[qq + 1 ] -= k;
}
for ( int i = 1 ; i <= n; i++)
{
x += a[i];
m = Math.max(m, x);
}
return m;
}
public static void main(String[] args)
{
int n = 10 , q = 3 ;
int [] a = new int [n + 5 ];
ArrayList<ArrayList<Integer>> v= new ArrayList<>();
for ( int i = 0 ; i < q; i++)
v.add( new ArrayList<>());
v.get( 0 ).add( 1 );
v.get( 0 ).add( 5 );
v.get( 0 ).add( 3 );
v.get( 1 ).add( 4 );
v.get( 1 ).add( 8 );
v.get( 1 ).add( 7 );
v.get( 2 ).add( 6 );
v.get( 2 ).add( 9 );
v.get( 2 ).add( 1 );
System.out.println(max_sum(a, v, q, n));
}
}
|
Python3
def max_sum(a, v, q, n):
x = 0 ;
m = - 10 * * 9 ;
for i in range (q):
p = v[i][ 0 ][ 0 ];
q = v[i][ 0 ][ 1 ];
k = v[i][ 1 ];
a[p] + = k;
if (q + 1 < = n):
a[q + 1 ] - = k;
for i in range ( 1 , n + 1 ):
x + = a[i];
m = max (m, x);
return m;
n = 10
q = 3 ;
a = [ 0 ] * (n + 5 );
v = [[[ 0 for i in range ( 2 )] for x in range ( 2 )] for z in range (q)]
v[ 0 ][ 0 ][ 0 ] = 1 ;
v[ 0 ][ 0 ][ 1 ] = 5 ;
v[ 0 ][ 1 ] = 3 ;
v[ 1 ][ 0 ][ 0 ] = 4 ;
v[ 1 ][ 0 ][ 1 ] = 8 ;
v[ 1 ][ 1 ] = 7 ;
v[ 2 ][ 0 ][ 0 ] = 6 ;
v[ 2 ][ 0 ][ 1 ] = 9 ;
v[ 2 ][ 1 ] = 1 ;
print (max_sum(a, v, q, n));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int max_sum( int [] a,
List<List< int >> v,
int q, int n)
{
int x = 0;
int m = int .MinValue;
for ( int i = 0; i < q; i++)
{
int p, qq, k;
p = v[i][0];
qq = v[i][1];
k = v[i][2];
a[p] += k;
if (qq + 1 <= n)
a[qq + 1] -= k;
}
for ( int i = 1; i <= n; i++)
{
x += a[i];
m = Math.Max(m, x);
}
return m;
}
public static void Main( string [] args)
{
int n = 10, q = 3;
int [] a = new int [n + 5];
List<List< int >> v = new List<List< int >>();
for ( int i = 0; i < q; i++)
v.Add( new List< int >());
v[0].Add(1);
v[0].Add(5);
v[0].Add(3);
v[1].Add(4);
v[1].Add(8);
v[1].Add(7);
v[2].Add(6);
v[2].Add(9);
v[2].Add(1);
Console.WriteLine(max_sum(a, v, q, n));
}
}
|
Javascript
<script>
function max_sum(a, v, q, n) {
let x = 0;
let m = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < q; i++) {
let p, q, k;
p = v[i][0][0];
q = v[i][0][1];
k = v[i][1];
a[p] += k;
if (q + 1 <= n)
a[q + 1] -= k;
}
for (let i = 1; i <= n; i++) {
x += a[i];
m = Math.max(m, x);
}
return m;
}
let n = 10, q = 3;
let a = new Array(n + 5).fill(0);
let v = new Array(q).fill(0).map(() => new Array(2).fill(0).map(() => new Array(2).fill(0)));
v[0][0][0] = 1;
v[0][0][1] = 5;
v[0][1] = 3;
v[1][0][0] = 4;
v[1][0][1] = 8;
v[1][1] = 7;
v[2][0][0] = 6;
v[2][0][1] = 9;
v[2][1] = 1;
document.write(max_sum(a, v, q, n));
</script>
|
Time Complexity: O(N+K) where N is the size of array and K is a number of queries
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...