Maximum Sum Subarray in O(n^2) Time Complexity using C++


Given an input array that may contain both positive and negative integers, find the sum of continuous subarray of numbers which has the largest sum.

Example:

Input: nums = [-2, -5, 6, -2, -3, 1, 5, -6]
Output: 7
Explanation: sum([6, -2, -3, 1, 5]) = 7

Note:

Your algorithm should run in O(n^2) time and use O(1) extra space.


Understanding the Problem

The core challenge of this problem is to find the subarray with the maximum sum in an array that contains both positive and negative integers. This problem is significant in various fields such as finance (to find the best time to buy and sell stocks) and computer science (for optimization problems).

Potential pitfalls include misunderstanding the definition of a subarray (which must be contiguous) and not considering negative numbers correctly.

Approach

To solve this problem, we can start with a naive approach and then optimize it:

Naive Approach

The naive approach involves checking all possible subarrays and calculating their sums. This can be done using two nested loops:

maxSum = nums[0]
for each i : 0 -> n - 1:
    for each j : i -> n - 1:
        maxSum = max(maxSum, sum(nums[i...j]))
return maxSum

However, this approach is not optimal as it has a time complexity of O(n^3) due to the nested loops and the sum calculation inside the inner loop.

Optimized Approach

We can optimize the naive approach by calculating the sum of subarrays in O(1) time using a running sum:

maxSum = nums[0]
for each i : 0 -> n - 1:
    sum = 0
    for each j : i -> n - 1:
        sum += nums[j]
        maxSum = max(maxSum, sum)
return maxSum

This approach reduces the time complexity to O(n^2) while maintaining O(1) space complexity.

Algorithm

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

  1. Initialize maxSum to the first element of the array.
  2. Use a nested loop to iterate over all possible subarrays.
  3. In the inner loop, maintain a running sum of the current subarray.
  4. Update maxSum if the current subarray sum is greater than maxSum.
  5. Return maxSum after all iterations.

Code Implementation

#include <iostream>
#include <vector>
#include <algorithm> // for std::max

int maxSubArraySum(const std::vector<int>& nums) {
    int maxSum = nums[0]; // Initialize maxSum to the first element
    int n = nums.size();
    
    for (int i = 0; i < n; ++i) {
        int sum = 0; // Initialize sum for the current subarray
        for (int j = i; j < n; ++j) {
            sum += nums[j]; // Add the current element to the sum
            maxSum = std::max(maxSum, sum); // Update maxSum if needed
        }
    }
    
    return maxSum; // Return the maximum subarray sum
}

int main() {
    std::vector<int> nums = {-2, -5, 6, -2, -3, 1, 5, -6};
    std::cout << "Maximum Sum Subarray: " << maxSubArraySum(nums) << std::endl;
    return 0;
}

Complexity Analysis

The time complexity of the optimized approach is O(n^2) due to the nested loops. The space complexity is O(1) as we are using only a few extra variables.

Edge Cases

Consider the following edge cases:

  • All negative numbers: The algorithm should return the least negative number.
  • Single element array: The algorithm should return that element.
  • Mixed positive and negative numbers: The algorithm should correctly identify the subarray with the maximum sum.

Testing

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

Test Case 1:
Input: [1, 2, 3, 4, 5]
Expected Output: 15

Test Case 2:
Input: [-1, -2, -3, -4, -5]
Expected Output: -1

Test Case 3:
Input: [3, -2, 5, -1]
Expected Output: 6

Test Case 4:
Input: [1]
Expected Output: 1

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

  • Understand the problem requirements and constraints thoroughly.
  • Start with a brute force approach to get a basic solution.
  • Look for patterns and ways to optimize the solution.
  • Practice similar problems to improve problem-solving skills.

Conclusion

In this blog post, we discussed the problem of finding the maximum sum subarray in an array containing both positive and negative integers. We explored a naive approach and an optimized approach, provided a detailed algorithm, and implemented the solution in C++. Understanding and solving such problems is crucial for improving problem-solving skills and preparing for coding interviews.

Additional Resources

For further reading and practice, consider the following resources: