Medium Dynamic Programming

Maximum Subarray

Receives an array of integers and returns the sum of the contiguous subarray with the largest sum. It solves this with Kadane's algorithm — a single dynamic-programming pass that, at each index, decides whether extending the running subarray from the previous index is better than starting a new one at the current element (currentSum = max(arr[i], currentSum + arr[i])), while separately tracking the best sum seen so far. Returns that single integer, the largest sum of any contiguous subarray; an empty array returns 0.

Visualization

Input

Algorithm code

Custom input

Saved inputs

References