# Maximum Sum of Two Non-Overlapping Subarrays in C++

Suppose we have an array A of integers; we have to find the maximum sum of elements in two non−overlapping subarrays. The lengths of these subarrays are L and M.

So more precisely we can say that, we have to find the largest V for which

V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either −

• 0 <= i < i + L − 1 < j < j + M − 1 < size of A, or

• 0 <= j < j + M − 1 < i < i + L − 1 < size of A.

To solve this, we will follow these steps −

• n := size of A

• Define an array leftL of size n, Define an array leftM of size n

• Define an array rightL of size n, Define an array rightM of size n

• ret := 0, temp := 0

• for initializing i := 0, when i < L, increase i by 1 do −

• temp = temp + A[i]

• for initializing i := L, j := 0, when i < n, increase i by 1, increase j by 1, do −

• leftL[i − 1] := max of temp and x where x is 0 when i − 2 < 0 otherwise x = leftL[i − 2]

• temp = temp + A[i]

• temp = temp − A[j]

• leftL[n − 1] := max of temp, and x where x is 0 when n − 2 < 0 otherwise x := leftL[n − 2]

• temp := 0

• for initializing i := 0, when i < M, increase i by 1 do −

• temp = temp + A[i]

• for initializing i := M, j := 0, when i < n, increase i by 1, increase j by 1, do −

• temp = temp + A[i]

• temp = temp - A[j]

• leftM[n − 1] := max of temp and x when x := 0 if n - 2 < 0 otherwise x := leftM[n − 2]

• temp := 0

• for initializing i := n − 1, when i > n − 1 − L, decrease i by 1 do −

• temp = temp + A[i]

• for initializing i := n − 1 − L, j := n − 1, when i >= 0, decrease i by 1, decrease j by 1, do −

• rightL[i + 1] := max of temp and x where x is 0 if i + 2 >= n otherwise x = rightL[i + 2]

• temp = temp + A[i]

• temp = temp − A[j]

• rightL[0] := max of temp and rightL[1]

• temp := 0

• for initializing i := n − 1, when i > n − 1 − M, decrease i by 1 do −

• temp = temp + A[i]

• for initializing i := n − 1 − M, j := n − 1, when i >= 0, decrease i by 1, decrease j by 1, do −

• rightM[i + 1] := max of temp and x, where x is 0 when i + 2 >= n otherwise x := rightM[i + 2]

• temp = temp + A[i]

• temp = temp − A[j]

• rightM[0] := max of temp and rightM[1]

• for initializing i := L − 1, when i <= n − 1 − M, increase i by 1 do −

• ret := max of ret and leftL[i] + rightM[i + 1]

• for initializing i := M − 1, when i <= n − 1 − L, increase i by 1 do −

• ret := max of ret and leftM[i] + rightL[i + 1]

• return ret

Let us see the following implementation to get better understanding −

## Example

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maxSumTwoNoOverlap(vector<int>& A, int L, int M) {
int n = A.size();
vector <int> leftL(n);
vector <int> leftM(n);
vector <int> rightL(n);
vector <int> rightM(n);
int ret = 0;
int temp = 0;
for(int i = 0; i < L; i++){
temp += A[i];
}
for(int i = L, j = 0; i < n; i++, j++){
leftL[i − 1] = max(temp, i − 2 < 0 ? 0 : leftL[i − 2]);
temp += A[i];
temp −= A[j];
}
leftL[n − 1] = max(temp, n − 2 < 0 ? 0 : leftL[n − 2]);
temp = 0;
for(int i = 0; i < M; i++){
temp += A[i];
}
for(int i = M, j = 0; i < n; i++, j++){
leftM[i − 1] = max(temp, i − 2 < 0 ? 0 : leftM[i − 2]);
temp += A[i];
temp −= A[j];
}
leftM[n − 1] = max(temp, n − 2 < 0 ? 0 : leftM[n − 2]);
//out(leftM);
temp = 0;
for(int i = n − 1; i > n − 1 − L; i−−){
temp += A[i];
}
for(int i = n − 1 − L, j = n − 1; i >= 0 ; i−−, j−− ){
rightL[i + 1] = max(temp, (i + 2 >= n ? 0 : rightL[i + 2]));
temp += A[i];
temp −= A[j];
}
rightL[0] = max(temp, rightL[1]);
temp = 0;
for(int i = n − 1; i > n − 1 − M; i−−){
temp += A[i];
}
for(int i = n − 1 − M, j = n − 1; i >= 0 ; i−−, j−− ){
rightM[i + 1] = max(temp, (i + 2 >= n ? 0 : rightM[i + 2]));
temp += A[i];
temp −= A[j];
}
rightM[0] = max(temp, rightM[1]);
for(int i = L − 1; i <= n − 1 − M; i++){
ret = max(ret, leftL[i] + rightM[i + 1]);
}
for(int i = M − 1; i <= n − 1 − L; i++){
ret = max(ret, leftM[i] + rightL[i + 1]);
}
return ret;
}
};
main(){
Solution ob;
vector<int> v1 = {0,6,5,2,3,5,1,9,4};
cout << (ob.maxSumTwoNoOverlap(v1, 1, 2));
}

## Input

[0,6,5,2,3,5,1,9,4]
1
2

20