Maximum Subarray (Kadane's Algorithm)

easy
Unit 2 · Stacks & Dynamic Programming0/3 to milestone

Find the contiguous run of numbers with the largest sum, such as the best stretch of daily profit and loss. Kadane's algorithm does it in one pass: at each position, either extend the best run ending at the previous position or start fresh.

Implement max_subarray(nums) returning (best_sum, start, end), where nums[start:end+1] is the best run. The run must contain at least one element, so all-negative input returns the largest single value. If several runs tie, return the one that starts first, and among those the shortest. Raise ValueError for an empty list.

#dynamic-programming#arrays2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Maximum Subarray (Kadane's Algorithm) · TechGradify