53. Maximum Subarray
Asked at Google, Meta, Amazon, Microsoft and 7 more
Difficulty
Medium
Time
O(n)
Space
O(1)
Pattern
Array
Companies
11
Problem
You are given an array of integers nums. Find one contiguous block of elements (a subarray) whose sum is as large as possible, and return that sum.
A subarray must be a run of neighbouring elements — you cannot skip elements or pick them from different places. The block must contain at least one element, so an empty subarray is not allowed.
The array may contain negative numbers, and it may be all negative. In that case the best block is the single largest element, because adding any other element would only make the sum smaller.
Pick one unbroken block
Constraints
- ●
1 <= nums.length <= 10^5 - ●
-10^4 <= nums[i] <= 10^4
Examples
nums = [-2,1,-3,4,-1,2,1,-5,4]6nums = [1]1nums = [5,4,-1,7,8]23Asked At
11 companies| Company | Difficulty | |
|---|---|---|
| Medium | View all Google questions → | |
| Meta | Medium | View all Meta questions → |
| Amazon | Medium | View all Amazon questions → |
| Microsoft | Medium | View all Microsoft questions → |
| Apple | Medium | View all Apple questions → |
| Netflix | Medium | View all Netflix questions → |
| Adobe | Medium | View all Adobe questions → |
| Atlassian | Medium | View all Atlassian questions → |
| Walmart | Medium | View all Walmart questions → |
| Bloomberg | Medium | View all Bloomberg questions → |
| Medium | View all LinkedIn questions → |
How to Think About It
At each position, you have two choices: extend the previous subarray or start fresh here. If the previous sum is negative, starting fresh is better.
The recurrence: maxEndingHere = max(nums[i], maxEndingHere + nums[i]). Either start a new subarray at nums[i] or extend the existing one.
Track the global maximum as you go. maxSoFar = max(maxSoFar, maxEndingHere). One pass through the array.
Why it works: maxEndingHere represents the best subarray ending at the current position. If the running sum goes negative, it's never worth extending — start fresh.
Edge cases: all negative numbers (return the largest single element), single element (return it).
Optimal Approach
Kadane's algorithm:
Step 1: Initialize maxEndingHere = nums[0], maxSoFar = nums[0].
Step 2: For each num in nums[1:]:
maxEndingHere = max(num, maxEndingHere + num)
maxSoFar = max(maxSoFar, maxEndingHere)
Step 3: Return maxSoFar.
The key insight: if maxEndingHere becomes negative, it's better to start fresh at the next element. A negative running sum can only hurt the next subarray.
Time: O(n). Space: O(1).
Kadane's algorithm in one pass
What Trips People Up in Real Interviews
Confusing "maximum subarray" with "maximum element." The subarray must be contiguous, and you're looking for the maximum sum, not the maximum single element.
Not handling all-negative arrays. If all elements are negative, the answer is the largest (least negative) single element, not 0.
Trying to use a separate array for dp. Kadane's algorithm can be done in O(1) space with just two variables.
Forgetting to initialize the result. Set result = nums[0] (or negative infinity), not 0. If all elements are negative, 0 would be wrong.
Thinking the subarray must start at index 0. Kadane's allows the maximum subarray to start anywhere — it tracks the best ending at each position and the global best across all positions.
Solution Code
O(n) timedef maxSubArray(nums):
max_ending_here = max_so_far = nums[0]
for num in nums[1:]:
max_ending_here = max(num, max_ending_here + num)
max_so_far = max(max_so_far, max_ending_here)
return max_so_farFrequently Asked Questions
What is the Maximum Subarray problem?
Given an integer array `nums`, find the subarray with the largest sum and return its sum. This is Kadane's algorithm, one of the most elegant DP solutions.
How do you solve Maximum Subarray?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Maximum Subarray?
Maximum Subarray is asked at Google, Meta, Amazon, Microsoft, Apple, Netflix, Adobe, Atlassian, Walmart, Bloomberg, LinkedIn. It is a medium difficulty problem.
What are common mistakes on Maximum Subarray?
- Confusing "maximum subarray" with "maximum element." The subarray must be contiguous, and you're looking for the maximum sum, not the maximum single element.
- Not handling all-negative arrays. If all elements are negative, the answer is the largest (least negative) single element, not 0.
- Trying to use a separate array for `dp`. Kadane's algorithm can be done in `O(1)` space with just two variables.
- Forgetting to initialize the result. Set result = `nums[0]` (or negative infinity), not 0. If all elements are negative, 0 would be wrong.
- Thinking the subarray must start at index 0. Kadane's allows the maximum subarray to start anywhere — it tracks the best ending at each position and the global best across all positions.