Skip to content
AI360Xpert
Beta
Difficulty: MediumGreedy

Maximum Subarray

Problem in Plain English

Given an integer array, find the largest possible sum of any contiguous block inside it. The block must contain at least one element and may be the whole array.

Problem Statement

Given an integer array nums, find the contiguous subarray with the largest sum and return that sum. A subarray must keep the original order and stay unbroken.

For nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] the answer is 6 from the block [4, -1, 2, 1]. A single element also counts, so nums = [1] returns 1.

Constraints

  • 1≤n=nums.length≤1051 \leq n = \mathrm{nums.length} \leq 10^5.
  • −104≤nums[i]≤104-10^4 \leq \mathrm{nums}[i] \leq 10^4.
  • The chosen subarray must contain at least one element.

Examples

Example 1

Input
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output
6

The block [4, -1, 2, 1] sums to 6, the largest of any contiguous block.

Example 2

Input
nums = [1]
Output
1

The only block is [1] itself.

Example 3

Input
nums = [5, 4, -1, 7, 8]
Output
23

The whole array sums to 23, which beats every shorter block.

Example 4

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

A nonempty block is required. The single value -1 beats every longer block; choosing an empty block with sum zero is illegal.

Intuition

A contiguous block cannot skip a negative value in its middle, but it can discard a harmful prefix. In Example 1, the sum before 4 is negative, so starting fresh at 4 beats extending it. The later -1 stays inside [4, -1, 2, 1] because the running sum remains positive and can help the next value. Track both the best block ending at the current index and the best block seen anywhere; these are different quantities.

Approaches

All Subarrays Scan

Solution Details

Reveal All Subarrays Scan: intuition, complexity, and code

Hints

Hint 1
Decide for each position whether the best block ending there extends the past or starts over.
Hint 2
Ask when carrying the running total forward can only hurt the future.
Hint 3
Track the best value seen at any ending instead of remembering where blocks began.

Edge Cases

  • All numbers negative, as in [-3, -1, -2], where the answer is the largest single element -1.
  • Single element [1], where the only block is the answer.
  • Entire array positive, where the answer is the whole-array sum.

Common Mistakes and Interview Tips

  • Initializing best to zero admits an empty block and fails on [-3, -1, -2]. Seed it with an actual element.
  • Resetting a negative total before recording it as a candidate can lose the largest negative element. This code resets before adding and updates best immediately after adding.
  • Returning the final cur reports only the best block ending at the last index. Example 1 ends at 5 but the answer is 6.
  • Dropping every negative value changes the problem to a subsequence. The optimal block may include negatives such as -1 in Example 1.

Key Takeaway

When segments must stay contiguous, restart the running total the moment it turns negative and remember the best ending anywhere.