Maximum Subarray Sum Problem

Maximum Subarray Sum Problem

23 Sep 2025
Beginner
495 Views
3 min read
Learn with an interactive course and practical hands-on labs

Free DSA Online Course with Certification

The Maximum Subarray Sum Problem is a classic algorithmic problem where you are given an array of integers (which may include negative numbers) and need to find the contiguous subarray with the largest sum. Return the maximum sum. This problem is efficiently solved using Kadane’s Algorithm. 

85% of top tech companies prioritize DSA expertise in hiring. Start your journey with our Free Data Structures and Algorithms Course now!

Example:

  • Input: nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
  • Output: 6
  • Explanation: The subarray [4, -1, 2, 1] has the largest sum = 6.

Logic

Enter an array of numbers (comma-separated) to find the maximum subarray sum.

Program (Python - Kadane’s Algorithm)

The following Python code uses Kadane’s Algorithm to solve the Maximum Subarray Sum Problem efficiently with O(n) time complexity.


def maxSubArray(nums):
    max_sum = nums[0]
    current_sum = nums[0]
    
    for num in nums[1:]:
        current_sum = max(num, current_sum + num)
        max_sum = max(max_sum, current_sum)
    
    return max_sum

# Example usage
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
print(maxSubArray(nums))  # Output: 6

Output

For the input nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4], the output is:

6

Explanation:

The subarray [4, -1, 2, 1] has the maximum sum of 6.

Top developers are already moving into Solution Architect roles. If you stay just a “coder,” you’ll be stuck while they lead. Enroll now in our Java Solution Architect Certification and step up.

Take our Datastructures skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Amit Kumar Ghosh (SDE and Mentor)

As a software developer with a wealth of experience, he brings a unique combination of technical acumen and a passion for mentorship to my role. With 6 years of experience, he has honed the skills in C/C++, Java, Python, SQL, C#, JavaScript, React, Java Spring etc. and has a proven track record of delivering high-quality, scalable software solutions and core Computer fundamental knowledge DSA, OOPs, CN, OS etc.

As a teacher, his approach revolves around interactive techniques, prioritizing hands-on learning and real-world projects. He explains concepts clearly, offer practical examples, and encourage questions to foster a supportive environment.
Live Training - Book Free Demo
ASP.NET Core Certification Training
25 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
25 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Solution Architect Certification Training
26 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Microservices Certification Training
26 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack Java Developer Certification Training Course
01 Nov
05:30PM - 07:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this