Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Maximum Subarray Sum

Working With Arrayshard

Course · Section 11: Working With Arrays · Lecture 149: Simple Array Methods

Implement `maxSubArray(nums)` returning the largest sum of any contiguous subarray (Kadane's algorithm).

Sample tests

Input: maxSubArray([-2,1,-3,4,-1,2,1,-5,4])
Output: 6
Input: maxSubArray([1])
Output: 1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Initializing the best to 0 breaks all-negative inputs; start from the first element.

Learning resources

  • MDN: Array
Approach & explanation (try first)

Kadane keeps the best sum ending at each index, resetting when extending hurts, and tracks the global best.

Loading...
⌘/Ctrl + Enter

Run your code to see results.