DSAPrep
MediumGreedy

Maximum Subarray

Given an integer array nums, find the subarray with the largest sum, and return its sum.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1

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.

Example 2

Input: nums = [1]
Output: 1

Example 3

Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The entire array is the best subarray.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
View original on LeetCode β†—

Brute Force

Time O(nΒ²)Space O(1)

For every starting index, extend the subarray one element at a time and track the running total, updating the best sum seen anywhere.

class Solution:
def maxSubArray(self, nums: list[int]) -> int:
n = len(nums)
best = float('-inf')
for i in range(n):
total = 0
for j in range(i, n):
total += nums[j]
best = max(best, total)
return best

Every subarray sum is computed independently β€” O(nΒ²) work in total, and it never reuses the fact that a sum ending at j-1 tells you almost everything about a sum ending at j.

Kadane's Algorithm

OptimalTime O(n)Space O(1)

For a subarray ending exactly at index i, the best possible sum is either nums[i] alone, or nums[i] plus whatever the best sum ending at i-1 was β€” if that previous best is negative, it can only hurt you, so drop it and restart from nums[i]. Track that β€œbest sum ending here” as you scan, and keep a running maximum of it.

class Solution:
def maxSubArray(self, nums: list[int]) -> int:
best = float('-inf')
cur = 0
for num in nums:
cur = max(num, cur + num)
best = max(best, cur)
return best

Tracing nums = [-2,1,-3,4,-1,2,1,-5,4]:

i
-2
0
1
1
-3
2
4
3
-1
4
2
5
1
6
-5
7
4
8
cur = -2best = -2
1 / 8
seenresultcurrentdiscarded

cur = max(-2, 0-2) = -2. Negative running sum β€” next step will restart.

Why it’s correct: at every index we ask one local question β€” β€œdoes carrying the previous run forward help or hurt?” β€” and a negative running sum can never help a future sum, so dropping it loses nothing. Complexity: one pass, two running variables β†’ O(n) time, O(1) space.