Given a finite sequence of real numbers, find a nonempty contiguous block with maximum sum. Its restriction to contiguous blocks distinguishes it from choosing an arbitrary subset. The problem admits a linear-time solution using Kadane's algorithm.
For input , let be the largest sum of a nonempty contiguous block ending at . The recurrence relation and is exact: an optimal block either starts at or extends an optimal block ending at . The answer is . Keeping only the current and best value gives linear time complexity and constant extra space complexity.

Articles by others on the same topic (1)

The Maximum Subarray Problem is a classic algorithmic problem that involves finding the contiguous subarray within a one-dimensional array of numbers that has the largest sum. In other words, given an array of integers (which can include both positive and negative numbers), the goal is to identify the subarray (a contiguous segment of the array) that yields the highest possible sum.