Given an array nums of integers, find three non-overlapping subarrays with maximum sum.
Return the total sum of the three subarrays
Example:
Input: [2, 3, -8, 7, -2, 9, -9, 7, -2, 4] Output: 28 Explanation: Subarrays [2, 3], [7, -2, 9] and [7, -2, 4] have the maximum sum of 28
Note:
The core challenge of this problem is to find three non-overlapping subarrays that together have the maximum possible sum. This problem is significant in scenarios where we need to maximize the sum of multiple segments of data, such as in financial analysis or signal processing.
Potential pitfalls include overlapping subarrays and not considering all possible subarray combinations.
To solve this problem, we can break it down into manageable steps:
A naive solution would involve checking all possible combinations of three subarrays, but this approach is not optimal due to its high time complexity.
We can optimize the solution using dynamic programming:
Here is a step-by-step breakdown of the algorithm:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to calculate the maximum sum of three non-overlapping subarrays
int maxSumOfThreeSubarrays(vector<int>& nums) {
int n = nums.size();
vector<int> prefixSum(n + 1, 0);
// Calculate prefix sums
for (int i = 0; i < n; ++i) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
// Arrays to store the maximum sum of subarrays
vector<int> leftMax(n, 0), rightMax(n, 0);
int maxSum = INT_MIN;
// Calculate the maximum sum of subarrays ending at or before each index
for (int i = 0; i < n; ++i) {
if (i >= 2) {
leftMax[i] = max(leftMax[i - 1], prefixSum[i + 1] - prefixSum[i - 2]);
} else {
leftMax[i] = prefixSum[i + 1];
}
}
// Calculate the maximum sum of subarrays starting at or after each index
for (int i = n - 1; i >= 0; --i) {
if (i <= n - 3) {
rightMax[i] = max(rightMax[i + 1], prefixSum[i + 3] - prefixSum[i]);
} else {
rightMax[i] = prefixSum[n] - prefixSum[i];
}
}
// Find the maximum sum of three non-overlapping subarrays
for (int i = 2; i < n - 2; ++i) {
maxSum = max(maxSum, leftMax[i - 1] + (prefixSum[i + 2] - prefixSum[i]) + rightMax[i + 2]);
}
return maxSum;
}
int main() {
vector<int> nums = {2, 3, -8, 7, -2, 9, -9, 7, -2, 4};
cout << "Maximum sum of three non-overlapping subarrays: " << maxSumOfThreeSubarrays(nums) << endl;
return 0;
}
The time complexity of this approach is O(n) because we iterate through the array a constant number of times. The space complexity is also O(n) due to the additional arrays used for prefix sums and maximum sums.
Potential edge cases include:
Each algorithm handles these cases by correctly calculating the prefix sums and updating the maximum sums accordingly.
To test the solution comprehensively, consider the following test cases:
Use testing frameworks like Google Test for automated testing.
When approaching such problems:
Understanding and solving problems like this one is crucial for developing strong problem-solving skills. Practice regularly and explore different approaches to enhance your understanding.
For further reading and practice: