Start learning free

LESSON 240 OF 410 · PARTIAL SUMS #2

Maximum Sum of Three Non-Overlapping Subarrays III in JavaScript (Time Complexity: O(n))

Practice O(n2) Read the full explanation

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:

  • Subarrays must be non-empty
  • nums contains at least three numbers
STEP 1 OF 22
    Ctrl + Enter to check

    JAVASCRIPT · PARTIAL SUMS #2

    Max Sum Of Three Subarrays in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to find three non-overlapping subarrays that together yield 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.

    Approach

    To solve this problem, we can break it down into manageable steps:

    1. Calculate the sum of all possible subarrays of a given length.
    2. Use dynamic programming to keep track of the best subarray sums up to each point in the array.
    3. Combine these results to find the maximum sum of three non-overlapping subarrays.

    Let's start with a naive approach and then optimize it.

    Naive Approach

    The naive approach would involve checking all possible combinations of three subarrays, which is computationally expensive and not feasible for large arrays.

    Optimized Approach

    We can optimize the solution using dynamic programming:

    1. Calculate prefix sums to quickly get the sum of any subarray.
    2. Use three arrays to store the best subarray sums up to each point in the array.
    3. Combine these results to find the maximum sum of three non-overlapping subarrays.

    Algorithm

    Here is a step-by-step breakdown of the optimized algorithm:

    1. Calculate the prefix sums of the array.
    2. Use three arrays to store the best subarray sums up to each point in the array.
    3. Iterate through the array to find the maximum sum of three non-overlapping subarrays.

    Code Implementation

    // Function to find the maximum sum of three non-overlapping subarrays
    function maxSumOfThreeSubarrays(nums) {
        const n = nums.length;
        const k = 3; // Length of each subarray
        const prefixSums = new Array(n + 1).fill(0);
    
        // Calculate prefix sums
        for (let i = 0; i < n; i++) {
            prefixSums[i + 1] = prefixSums[i] + nums[i];
        }
    
        // Arrays to store the best subarray sums up to each point
        const left = new Array(n).fill(0);
        const right = new Array(n).fill(0);
    
        // Calculate the best subarray sums from the left
        let maxSum = prefixSums[k] - prefixSums[0];
        for (let i = k; i < n; i++) {
            if (prefixSums[i + 1] - prefixSums[i + 1 - k] > maxSum) {
                maxSum = prefixSums[i + 1] - prefixSums[i + 1 - k];
                left[i] = i + 1 - k;
            } else {
                left[i] = left[i - 1];
            }
        }
    
        // Calculate the best subarray sums from the right
        maxSum = prefixSums[n] - prefixSums[n - k];
        right[n - k] = n - k;
        for (let i = n - k - 1; i >= 0; i--) {
            if (prefixSums[i + k] - prefixSums[i] >= maxSum) {
                maxSum = prefixSums[i + k] - prefixSums[i];
                right[i] = i;
            } else {
                right[i] = right[i + 1];
            }
        }
    
        // Find the maximum sum of three non-overlapping subarrays
        let result = [];
        maxSum = 0;
        for (let i = k; i <= n - 2 * k; i++) {
            const l = left[i - 1];
            const r = right[i + k];
            const total = (prefixSums[i + k] - prefixSums[i]) + (prefixSums[l + k] - prefixSums[l]) + (prefixSums[r + k] - prefixSums[r]);
            if (total > maxSum) {
                maxSum = total;
                result = [l, i, r];
            }
        }
    
        return maxSum;
    }
    
    // Example usage
    const nums = [2, 3, -8, 7, -2, 9, -9, 7, -2, 4];
    console.log(maxSumOfThreeSubarrays(nums)); // Output: 28
    

    Complexity Analysis

    The time complexity of this approach is O(n), where n is the length of the array. This is because we are iterating 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 tracking the best subarray sums.

    Edge Cases

    Potential edge cases include:

    • Arrays with negative numbers.
    • Arrays where the best subarrays are at the beginning or end.

    Each algorithm handles these cases by using prefix sums and dynamic programming to ensure all possible subarrays are considered.

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Simple cases with small arrays.
    • Arrays with all positive or all negative numbers.
    • Large arrays to test performance.

    Use testing frameworks like Jest or Mocha for automated testing.

    Thinking and Problem-Solving Tips

    When approaching such problems:

    • Break the problem into smaller, manageable parts.
    • Consider using dynamic programming for optimization.
    • Practice similar problems to improve problem-solving skills.

    Conclusion

    Understanding and solving problems like this one is crucial for developing strong algorithmic skills. Practice regularly and explore different approaches to enhance your problem-solving abilities.

    Additional Resources

    For further reading and practice:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free