Dashboard
LC #53

Maximum Subarray

Pattern · ?AmazonGoogleLinkedInMetaBloombergMicrosoft

Problem

Find the contiguous subarray with the largest sum (Kadane's algorithm).

Constraints

  • 1 ≤ nums.length ≤ 10⁵
  • -10⁴ ≤ nums[i] ≤ 10⁴

Example

Input
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output
6
Why
Subarray [4,-1,2,1] has the largest sum = 6

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗