Skip to content
BASA DSA

Maximum Subarray

MediumGreedy

Return the largest sum of a nonempty contiguous subarray.

Example 1

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

Explanation

Contiguous 4,-1,2 sums to 5; dropping the surrounding negatives is best.

Example 2

Input
nums = [-7,-2,-9]
Output
-2

Explanation

The subarray must be nonempty, so choose -2 rather than zero.

Example 3

Input
nums = [5]
Output
5

Explanation

The only nonempty subarray contains 5.

Constraints

  • 1 <= len(nums) <= 100000
  • Values are -10000..10000

Function contractmaxSubArrayArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…