Find the contiguous subarray with the largest possible sum.
Given an integer array, choose a contiguous subarray with the maximum possible sum and return that sum.
A subarray must contain at least one element.
nums.nums[i] may be positive, negative, or zero.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, which is 6.
Example 2
Input
nums = [1]
Output
1
Explanation
The only subarray is [1].
Example 3
Input
nums = [-8,-3,-6,-2,-5,-4]
Output
-2
Explanation
All numbers are negative, so the best choice is the largest single element.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.