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


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 move to more optimized solutions.

Naive Approach

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

int maxSum = nums[0];
for (int i = 0; i < nums.length; i++) {
    for (int j = i; j < nums.length; j++) {
        int sum = 0;
        for (int k = i; k <= j; k++) {
            sum += nums[k];
        }
        maxSum = Math.max(maxSum, sum);
    }
}
return maxSum;

However, this approach has a time complexity of O(n^3), which is not efficient for large arrays.

Optimized Approach

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

int maxSum = nums[0];
for (int i = 0; i < nums.length; i++) {
    int sum = 0;
    for (int j = i; j < nums.length; j++) {
        sum += nums[j];
        maxSum = Math.max(maxSum, sum);
    }
}
return maxSum;

This 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 with the first element of the array.
  2. Use a nested loop to iterate through 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

public class MaximumSumSubarray {
    public static int maxSubArray(int[] nums) {
        // Initialize maxSum with the first element of the array
        int maxSum = nums[0];
        
        // Iterate through each possible starting point of the subarray
        for (int i = 0; i < nums.length; i++) {
            int sum = 0;
            // Iterate through each possible ending point of the subarray
            for (int j = i; j < nums.length; j++) {
                // Add the current element to the running sum
                sum += nums[j];
                // Update maxSum if the current subarray sum is greater
                maxSum = Math.max(maxSum, sum);
            }
        }
        return maxSum;
    }

    public static void main(String[] args) {
        int[] nums = {-2, -5, 6, -2, -3, 1, 5, -6};
        System.out.println("Maximum Sum Subarray: " + maxSubArray(nums)); // Output: 7
    }
}

Complexity Analysis

The time complexity of the optimized approach is O(n^2) because of the two 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 maximum single element.
  • Single element array: The algorithm should return that element.
  • All positive numbers: The algorithm should return the sum of the entire array.

Examples:

Input: nums = [-1, -2, -3]
Output: -1

Input: nums = [5]
Output: 5

Input: nums = [1, 2, 3, 4]
Output: 10

Testing

To test the solution comprehensively, consider a variety of test cases:

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

Use a testing framework like JUnit to automate the testing process.

Thinking and Problem-Solving Tips

When approaching such problems:

  • Understand the problem requirements and constraints.
  • Start with a brute force solution to get a basic understanding.
  • 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 Java. 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: