# Maximum sum subarray of size range [L, R]

Given an integer array arr[] of size N and two integer L and R. The task is to find the maximum sum subarray of size between L and R (both inclusive).

Example:

Input: arr[] = {1, 2, 2, 1}, L = 1, R = 3
Output: 5
Explaination:
Subarray of size 1 are {1}, {2}, {2}, {1} and maximum sum subarray = 2 for subarray {2}.
Subarray of size 2 are {1, 2}, {2, 2}, {2, 1}, and maximum sum subarray = 4 for subarray {2, 2}.
Subarray of size 3 are {1, 2, 1}, {2, 2, 1}, and maximum sum subarray = 5 for subarray {2, 2, 1}.
Hence the maximum possible sum subarray is 5.

Input: arr[] = {-1, -3, -7, -11}, L = 1, R = 4
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Here we will use the concept of sliding window which is discuss in this post.
2. First calculate prefix sum of array in array pre[].
3. Next iterate over the range L to N -1, and consider all subarray of size L to R.
4. Create a multiset for storing prefix sums of subarray length L to R.
5. Now to find maximum sum subarray ending at index i just subtract pre[i] and minimum of all values from pre[i – L] to pre[i – R].
6. Finally return maximum of all sums.

Here is the implementation of the above approach:

## C++

 `// C++ program to find Maximum sum ` `// subarray of size between L and R. ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// function to find Maximum sum subarray ` `// of size between L and R ` `void` `max_sum_subarray(vector<``int``> arr, ` `                      ``int` `L, ``int` `R) ` `{ ` `    ``int` `n = arr.size(); ` `    ``int` `pre[n] = { 0 }; ` ` `  `    ``// calculating prefix sum ` `    ``pre[0] = arr[0]; ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``pre[i] = pre[i - 1] + arr[i]; ` `    ``} ` `    ``multiset<``int``> s1; ` ` `  `    ``// maintain 0 for initial ` `    ``// values of i upto R ` `    ``// Once i = R, then ` `    ``// we need to erase that 0 from ` `    ``// our multiset as our first ` `    ``// index of subarray ` `    ``// cannot be 0 anymore. ` `    ``s1.insert(0); ` `    ``int` `ans = INT_MIN; ` ` `  `    ``ans = max(ans, pre[L - 1]); ` ` `  `    ``// we maintain flag to ` `    ``// counter if that initial ` `    ``// 0 was erased from set or not. ` `    ``int` `flag = 0; ` ` `  `    ``for` `(``int` `i = L; i < n; i++) { ` ` `  `        ``// erase 0 from multiset once i=b ` `        ``if` `(i - R >= 0) { ` `            ``if` `(flag == 0) { ` ` `  `                ``auto` `it = s1.find(0); ` `                ``s1.erase(it); ` `                ``flag = 1; ` `            ``} ` `        ``} ` `        ``// insert pre[i-L] ` `        ``if` `(i - L >= 0) ` `            ``s1.insert(pre[i - L]); ` ` `  `        ``// find minimum value in multiset. ` `        ``ans = max(ans, ` `                  ``pre[i] - *s1.begin()); ` ` `  `        ``// erase pre[i-R] ` `        ``if` `(i - R >= 0) { ` `            ``auto` `it = s1.find(pre[i - R]); ` `            ``s1.erase(it); ` `        ``} ` `    ``} ` `    ``cout << ans << endl; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `L, R; ` `    ``L = 1; ` `    ``R = 3; ` `    ``vector<``int``> arr = { 1, 2, 2, 1 }; ` `    ``max_sum_subarray(arr, L, R); ` `    ``return` `0; ` `} `

## Python3

 `# Python3 program to find maximum sum ` `# subarray of size between L and R. ` `import` `sys ` ` `  `# Function to find maximum sum subarray ` `# of size between L and R ` `def` `max_sum_subarray(arr, L, R): ` ` `  `    ``n ``=` `len``(arr) ` `    ``pre ``=` `n ``*` `[``0``] ` ` `  `    ``# Calculating prefix sum ` `    ``pre[``0``] ``=` `arr[``0``] ` `    ``for` `i ``in` `range``(``1``, n): ` `        ``pre[i] ``=` `pre[i ``-` `1``] ``+` `arr[i] ` ` `  `    ``s1 ``=` `[] ` ` `  `    ``# Maintain 0 for initial ` `    ``# values of i upto R ` `    ``# Once i = R, then ` `    ``# we need to erase that 0 from ` `    ``# our multiset as our first ` `    ``# index of subarray ` `    ``# cannot be 0 anymore. ` `    ``s1.append(``0``) ` `    ``ans ``=` `-``sys.maxsize ``-` `1` ` `  `    ``ans ``=` `max``(ans, pre[L ``-` `1``]) ` ` `  `    ``# We maintain flag to ` `    ``# counter if that initial ` `    ``# 0 was erased from set or not. ` `    ``flag ``=` `0` ` `  `    ``for` `i ``in` `range``(L, n): ` `         `  `        ``# Erase 0 from multiset once i=b ` `        ``if` `(i ``-` `R >``=` `0``): ` `            ``if` `(flag ``=``=` `0``): ` `                ``s1.remove(``0``) ` `                ``flag ``=` `1` `     `  `        ``# Insert pre[i-L] ` `        ``if` `(i ``-` `L >``=` `0``): ` `            ``s1.append(pre[i ``-` `L]) ` ` `  `        ``# Find minimum value in multiset. ` `        ``ans ``=` `max``(ans, pre[i] ``-` `s1[``0``]) ` ` `  `        ``# Erase pre[i-R] ` `        ``if` `(i ``-` `R >``=` `0``): ` `            ``s1.remove(pre[i ``-` `R]) ` ` `  `    ``print``(ans) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``L ``=` `1` `    ``R ``=` `3` `    ``arr ``=` `[ ``1``, ``2``, ``2``, ``1` `] ` `     `  `    ``max_sum_subarray(arr, L, R) ` ` `  `# This code is contributed by chitranayal `

Output:

```5
```

Time Complexity: O (N * log N)
Auxiliary Space: O (N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : chitranayal