Skip to main content
LeetCode 53, Medium. Topics: Array, Divide and Conquer, Dynamic Programming. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

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

Examples

Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Explanation: The subarray [1] has the largest sum 1.
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Grind 75, Grind, Blind 75, NeetCode 150, NeetCode 250, NeetCode All, AlgoMaster 75.
Last modified on August 25, 2026