Start learning free

LESSON 229 OF 410 · PARTIAL SUMS #1

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

Practice O(n) Read the full explanation

( Prefix Sums Approach )


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) time and use at most O(n) extra space.


STEP 1 OF 7
    Ctrl + Enter to check

    JAVA · PARTIAL SUMS #1

    Maximum Sum Subarray in Java, explained

    Understanding the Problem

    The core challenge of this problem is to find the subarray (a contiguous part of the array) that has the maximum sum. 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 requirement for the subarray to be contiguous and not considering negative numbers correctly.

    Approach

    To solve this problem, we can use Kadane's Algorithm, which is an efficient way to find the maximum sum subarray in O(n) time complexity. The naive approach would involve checking all possible subarrays, which would be inefficient with a time complexity of O(n^2).

    Naive Approach

    The naive approach involves iterating through all possible subarrays and calculating their sums, which is not optimal due to its O(n^2) time complexity.

    Optimized Approach: Kadane's Algorithm

    Kadane's Algorithm improves upon the naive approach by maintaining a running sum of the current subarray and updating the maximum sum found so far. The key idea is to decide at each element whether to add it to the current subarray or start a new subarray.

    Algorithm

    Here is a step-by-step breakdown of Kadane's Algorithm:

    1. Initialize two variables: maxSoFar to store the maximum sum found so far and maxEndingHere to store the maximum sum of the subarray ending at the current position.
    2. Iterate through the array, updating maxEndingHere to be the maximum of the current element alone or the current element plus maxEndingHere.
    3. Update maxSoFar to be the maximum of maxSoFar and maxEndingHere.
    4. Return maxSoFar as the result.

    Code Implementation

    public class MaximumSumSubarray {
        public static int maxSubArray(int[] nums) {
            // Initialize variables to store the maximum sum so far and the maximum sum ending at the current position
            int maxSoFar = nums[0];
            int maxEndingHere = nums[0];
    
            // Iterate through the array starting from the second element
            for (int i = 1; i < nums.length; i++) {
                // Update maxEndingHere to be the maximum of the current element alone or the current element plus maxEndingHere
                maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
                // Update maxSoFar to be the maximum of maxSoFar and maxEndingHere
                maxSoFar = Math.max(maxSoFar, maxEndingHere);
            }
    
            // Return the maximum sum found
            return maxSoFar;
        }
    
        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 Kadane's Algorithm is O(n) because it involves a single pass through the array. The space complexity is O(1) as it uses a constant amount of extra space.

    Edge Cases

    Potential edge cases include:

    • All negative numbers: The algorithm should return the maximum single element.
    • Single element array: The algorithm should return that element.

    Examples:

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

    Testing

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

    • Simple cases with small arrays.
    • Arrays with all positive or all negative numbers.
    • Arrays with a mix of positive and negative numbers.
    • Edge cases with single element arrays.

    Thinking and Problem-Solving Tips

    When approaching such problems, it is helpful to:

    • Understand the problem requirements and constraints thoroughly.
    • Start with a brute-force solution to understand the problem better.
    • Look for patterns and optimizations to improve the solution.
    • Practice similar problems to develop problem-solving skills.

    Conclusion

    In this blog post, we discussed how to solve the Maximum Sum Subarray problem using Kadane's Algorithm. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. 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 JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free