
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
Output
20
- Related Articles
- Maximum sum two non-overlapping subarrays of given size in C++
- Maximum Sum of 3 Non-Overlapping Subarrays in C++
- Maximum sum of lengths of non-overlapping subarrays with k as the max element in C++
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Program to find maximum sum of two non-overlapping sublists in Python
- Max sum of M non-overlapping subarrays of size K in C++
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find maximum number of non-overlapping substrings in Python
- Minimum Size of Two Non-Overlapping Intervals in C++
- Program to find largest sum of 3 non-overlapping sublists of same sum in Python
- Program to find two non-overlapping sub-arrays each with target sum using Python
- Find the overlapping sum of two arrays using C++
- Non-overlapping Intervals in C++
- Largest sum of subarrays in JavaScript
- Random Point in Non-overlapping Rectangles in C++
