Maximize subarray sum of given Array by adding X in range [L, R] for Q queries
Last Updated :
06 Dec, 2021
Given an array arr[] of N integers and M update queries of the type (L, R, X), the task is to find the maximum subarray sum after each update query where in each query, add integer X to every element of the array arr[] in the range [L, R].
Examples:
Input: arr[] = {-1, 5, -2, 9, 3, -3, 2}, query[] = {{0, 2, -10}, {4, 5, 2}}
Output: 12 15
Explanation: Below are the steps to solve the above example:
- The array after 1st update query becomes arr[] = {-11, -5, -12, 9, 3, -3, 2}. Hence the maximum subarray sum is 12 of the subarray arr[3... 4].
- The array after 2nd update query becomes arr[] = {-11, -5, -12, 9, 5, -1, 2}. Hence the maximum subarray sum is 15 of the subarray arr[3... 6].
Input: arr[] = {-2, -5, 6, -2, -3, 1, 5, -6, 4, -1}, query[] = {{1, 4, 3}, {4, 5, -4}, {7, 9, 5}}
Output: 16 10 20
Approach: The given problem can be solved using Kadane's Algorithm. For each query, update the array elements by traversing over all the elements of the array arr[] in the range [L, R] and add integer X to each element. After every update query, calculate the maximum subarray sum using the algorithm discussed here.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum subarray
// sum using Kadane's Algorithm
int maxSubarraySum(int arr[], int n)
{
// Stores the maximum sum
int maxSum = INT_MIN;
int currSum = 0;
// Loop to iterate over the array
for (int i = 0; i <= n - 1; i++) {
currSum += arr[i];
// Update maxSum
if (currSum > maxSum) {
maxSum = currSum;
}
if (currSum < 0) {
currSum = 0;
}
}
// Return Answer
return maxSum;
}
// Function to add integer X to all elements
// of the given array in range [L, R]
void updateArr(int* arr, int L, int R, int X)
{
// Loop to iterate over the range
for (int i = L; i <= R; i++) {
arr[i] += X;
}
}
// Function to find the maximum subarray sum
// after each range update query
void maxSubarraySumQuery(
int arr[], int n,
vector<vector<int> > query)
{
// Loop to iterate over the queries
for (int i = 0; i < query.size(); i++) {
// Function call to update the array
// according to the mentioned query
updateArr(arr, query[i][0],
query[i][1],
query[i][2]);
// Print the max subarray sum after
// updating the given array
cout << maxSubarraySum(arr, n) << " ";
}
}
// Driver Code
int main()
{
int arr[] = { -2, -5, 6, -2, -3,
1, 5, -6, 4, -1 };
int N = sizeof(arr) / sizeof(arr[0]);
vector<vector<int> > query{ { 1, 4, 3 },
{ 4, 5, -4 },
{ 7, 9, 5 } };
maxSubarraySumQuery(arr, N, query);
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to find the maximum subarray
// sum using Kadane's Algorithm
public static int maxSubarraySum(int arr[], int n)
{
// Stores the maximum sum
int maxSum = Integer.MIN_VALUE;
int currSum = 0;
// Loop to iterate over the array
for (int i = 0; i <= n - 1; i++) {
currSum += arr[i];
// Update maxSum
if (currSum > maxSum) {
maxSum = currSum;
}
if (currSum < 0) {
currSum = 0;
}
}
// Return Answer
return maxSum;
}
// Function to add integer X to all elements
// of the given array in range [L, R]
public static void updateArr(int[] arr, int L,
int R, int X)
{
// Loop to iterate over the range
for (int i = L; i <= R; i++) {
arr[i] += X;
}
}
// Function to find the maximum subarray sum
// after each range update query
public static void maxSubarraySumQuery(int arr[], int n, int[][] query)
{
// Loop to iterate over the queries
for (int i = 0; i < query.length; i++)
{
// Function call to update the array
// according to the mentioned query
updateArr(arr, query[i][0],
query[i][1],
query[i][2]);
// Print the max subarray sum after
// updating the given array
System.out.print(maxSubarraySum(arr, n) + " ");
}
}
// Driver Code
public static void main(String args[])
{
int arr[] = { -2, -5, 6, -2, -3,
1, 5, -6, 4, -1 };
int N = arr.length;
int[][] query = { { 1, 4, 3 }, { 4, 5, -4 }, { 7, 9, 5 } };
maxSubarraySumQuery(arr, N, query);
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python Program to implement
# the above approach
# Function to find the maximum subarray
# sum using Kadane's Algorithm
def maxSubarraySum(arr, n):
# Stores the maximum sum
maxSum = 10 ** -9
currSum = 0
# Loop to iterate over the array
for i in range(n):
currSum += arr[i]
# Update maxSum
if (currSum > maxSum):
maxSum = currSum
if (currSum < 0):
currSum = 0
# Return Answer
return maxSum
# Function to add integer X to all elements
# of the given array in range[L, R]
def updateArr(arr, L, R, X):
# Loop to iterate over the range
for i in range(L, R + 1):
arr[i] += X
# Function to find the maximum subarray sum
# after each range update query
def maxSubarraySumQuery(arr, n, query):
# Loop to iterate over the queries
for i in range(len(query)):
# Function call to update the array
# according to the mentioned query
updateArr(arr, query[i][0],
query[i][1],
query[i][2])
# Print the max subarray sum after
# updating the given array
print(maxSubarraySum(arr, n), end=" ")
# Driver Code
arr = [-2, -5, 6, -2, -3, 1, 5, -6, 4, -1]
N = len(arr)
query = [[1, 4, 3],[4, 5, -4],[7, 9, 5]]
maxSubarraySumQuery(arr, N, query)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the maximum subarray
// sum using Kadane's Algorithm
public static int maxSubarraySum(int[] arr, int n)
{
// Stores the maximum sum
int maxSum = int.MinValue;
int currSum = 0;
// Loop to iterate over the array
for (int i = 0; i <= n - 1; i++)
{
currSum += arr[i];
// Update maxSum
if (currSum > maxSum)
{
maxSum = currSum;
}
if (currSum < 0)
{
currSum = 0;
}
}
// Return Answer
return maxSum;
}
// Function to add integer X to all elements
// of the given array in range [L, R]
public static void updateArr(int[] arr, int L,
int R, int X)
{
// Loop to iterate over the range
for (int i = L; i <= R; i++)
{
arr[i] += X;
}
}
// Function to find the maximum subarray sum
// after each range update query
public static void maxSubarraySumQuery(int[] arr, int n, int[,] query)
{
// Loop to iterate over the queries
for (int i = 0; i < query.Length; i++)
{
// Function call to update the array
// according to the mentioned query
updateArr(arr, query[i, 0],
query[i, 1],
query[i, 2]);
// Print the max subarray sum after
// updating the given array
Console.Write(maxSubarraySum(arr, n) + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { -2, -5, 6, -2, -3, 1, 5, -6, 4, -1 };
int N = arr.Length;
int[,] query = { { 1, 4, 3 }, { 4, 5, -4 }, { 7, 9, 5 } };
maxSubarraySumQuery(arr, N, query);
}
}
// This code is contributed by _saurabh_jaiswal.
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to find the maximum subarray
// sum using Kadane's Algorithm
function maxSubarraySum(arr, n)
{
// Stores the maximum sum
let maxSum = Number.MIN_VALUE;
let currSum = 0;
// Loop to iterate over the array
for (let i = 0; i <= n - 1; i++) {
currSum += arr[i];
// Update maxSum
if (currSum > maxSum) {
maxSum = currSum;
}
if (currSum < 0) {
currSum = 0;
}
}
// Return Answer
return maxSum;
}
// Function to add integer X to all elements
// of the given array in range [L, R]
function updateArr(arr, L, R, X) {
// Loop to iterate over the range
for (let i = L; i <= R; i++) {
arr[i] += X;
}
}
// Function to find the maximum subarray sum
// after each range update query
function maxSubarraySumQuery(
arr, n,
query) {
// Loop to iterate over the queries
for (let i = 0; i < query.length; i++) {
// Function call to update the array
// according to the mentioned query
updateArr(arr, query[i][0],
query[i][1],
query[i][2]);
// Print the max subarray sum after
// updating the given array
document.write(maxSubarraySum(arr, n) + " ");
}
}
// Driver Code
let arr = [-2, -5, 6, -2, -3,
1, 5, -6, 4, -1];
let N = arr.length;
let query = [[1, 4, 3],
[4, 5, -4],
[7, 9, 5]];
maxSubarraySumQuery(arr, N, query);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Similar Reads
Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types: If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array. Examples: Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2,
13 min read
Javascript Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types: If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array. Examples:Â Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2,
5 min read
Queries for Sum of Bitwise AND of all Subarrays in a Range Given an array arr[] of size N, the task is to answer a set of Q queries, each in the format of queries[i][0] and queries[i][1]. For each queries[i], find the sum of Bitwise AND of all subarrays whose elements lie in the range [queries[i][0], queries[i][1]]. Examples: Input: N = 3, arr[] = {1, 0, 2}
12 min read
Maximum sum among all (a x b) submatrices for given Q queries Given a matrix mat[][] of size N x M and an array queries[] of size Q, containing (a, b) pairs. The task is to find the maximum sum among all (a x b) sub-matrices of the matrix mat[][]. Note: The rows and columns of the sub-matrix must be contiguous. Examples: Input: N = 3, M = 4, Q = 1, queries[] =
15 min read
Maximize Array sum by adding multiple of another Array element in given ranges Given two arrays X[] and Y[] of length N along with Q queries each of type [L, R] that denotes the subarray of X[] from L to R. The task is to find the maximum sum that can be obtained by applying the following operation for each query: Choose an element from Y[]. Add multiples with alternate +ve an
15+ min read
Maximize array sum by replacing at most L elements to R for Q queries Given an array arr[] consisting of N integers and an array Query[][] consisting of M pairs of the type {L, R}, the task is to find the maximum sum of the array by performing the queries Query[][] such that for each query {L, R} replace at most L array elements to the value R. Examples: Input: arr[]=
7 min read
Maximum pair sum in the given index ranges of an Array Given an array arr containing N positive integers and the number of queries Q, for each query task is to find the maximum pair sum in the given index range [L, R] where L and R are the respective low and high indices.Examples: Input: arr = {3, 4, 5, 6, 7, 8}, Q[][2] = [[0, 3], [3, 5]] Output: 11 15
9 min read
Sum of all armstrong numbers lying in the range [L, R] for Q queries Given Q queries in the form of a 2D array arr[][] in which every row consists of two numbers L and R which denotes a range [L, R], the task is to find the sum of all Armstrong Numbers lying in the given range [L, R].Examples: Input: Q = 2, arr = [ [1, 13], [121, 211] ] Output: 45 153 Explanation: Fr
9 min read
Find maximum sum by replacing the Subarray in given range Given an array arr[], the task is to find the maximum sum possible such that any subarray of the indices from [l, r] i.e all subarray elements from arr[l] to arr[r] can be replaced with |arr[l] - arr[r]| any number of times. Examples: Input: arr[] = { 9, 1}Output: 16Explanation: The subarray [l, r]
9 min read
Find the Initial Array from given array after range sum queries Given an array arr[] which is the resultant array when a number of queries are performed on the original array. The queries are of the form [l, r, x] where l is the starting index in the array, r is the ending index in the array and x is the integer elements that have to be added to all the elements
13 min read