Classic example
Input
[-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output
6
The contiguous subarray [4, -1, 2, 1] has the largest sum 6.
Full lesson preview
Find the largest sum of any contiguous subarray in an integer array using dynamic programming (Kadane's pattern).
Problem statement
Task
Examples
Input
[-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output
6
The contiguous subarray [4, -1, 2, 1] has the largest sum 6.
Input format
Output format
Constraints
Samples
Input
[1, 2, 3, 4]
Output
10
All positives, so the whole array is the max subarray.