Start learning free

LESSON 230 OF 410 · PARTIAL SUMS #1

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

Practice O(n6) 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 16
    Ctrl + Enter to check

    JAVASCRIPT · PARTIAL SUMS #1

    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 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.

    Approach

    To solve this problem, we can break it down into smaller 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 solution and then optimize it.

    Naive Solution

    The naive solution involves checking all possible combinations of three subarrays. This approach is not optimal due to its high time complexity of O(n^3).

    Optimized Solution

    We can optimize the solution using dynamic programming. The idea is to precompute the best subarray sums for each possible starting point and then combine these results efficiently.

    Algorithm

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

    1. Compute the prefix sums of the array to quickly calculate subarray sums.
    2. Use three arrays to store the best subarray sums up to each point for the first, second, and third subarrays.
    3. Iterate through the array to update these arrays based on the prefix sums.
    4. Combine the results 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, k) {
        const n = nums.length;
        const sum = new Array(n + 1).fill(0);
        const left = new Array(n).fill(0);
        const right = new Array(n).fill(0);
    
        // Compute prefix sums
        for (let i = 0; i < n; i++) {
            sum[i + 1] = sum[i] + nums[i];
        }
    
        // Compute the best subarray sum for the left side
        let total = sum[k] - sum[0];
        for (let i = k; i < n; i++) {
            if (sum[i + 1] - sum[i + 1 - k] > total) {
                total = sum[i + 1] - sum[i + 1 - k];
                left[i] = i + 1 - k;
            } else {
                left[i] = left[i - 1];
            }
        }
    
        // Compute the best subarray sum for the right side
        right[n - k] = n - k;
        total = sum[n] - sum[n - k];
        for (let i = n - k - 1; i >= 0; i--) {
            if (sum[i + k] - sum[i] >= total) {
                total = sum[i + k] - sum[i];
                right[i] = i;
            } else {
                right[i] = right[i + 1];
            }
        }
    
        // Find the maximum sum by combining the results
        let maxSum = 0;
        let result = [-1, -1, -1];
        for (let i = k; i <= n - 2 * k; i++) {
            const l = left[i - 1];
            const r = right[i + k];
            total = (sum[i + k] - sum[i]) + (sum[l + k] - sum[l]) + (sum[r + k] - sum[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];
    const k = 2; // Length of each subarray
    console.log(maxSumOfThreeSubarrays(nums, k)); // Output: 28
    

    Complexity Analysis

    The time complexity of this optimized solution is O(n), where n is the length of the input array. This is because we make a constant number of passes through the array. 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.
    • Arrays with all positive or all negative numbers.

    Each of these cases should be tested to ensure the algorithm handles them correctly.

    Testing

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

    1. [2, 3, -8, 7, -2, 9, -9, 7, -2, 4], k = 2
    2. [1, 2, 1, 2, 6, 7, 5, 1], k = 2
    3. [1, 2, 3, 4, 5, 6, 7, 8, 9], k = 3
    4. [-1, -2, -3, -4, -5, -6, -7, -8, -9], k = 2
    

    Use a testing framework like Jest or Mocha to automate these tests.

    Thinking and Problem-Solving Tips

    When approaching such problems, consider breaking them down into smaller subproblems and using dynamic programming to optimize the solution. Practice similar problems to develop a deeper understanding of the techniques involved.

    Conclusion

    In this blog post, we discussed how to solve the problem of finding the maximum sum of three non-overlapping subarrays. We explored a naive solution and then optimized it using dynamic programming. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

    Additional Resources

    For further reading and practice, consider the following resources:

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