In computer science, the maximum sum subarray problem, also known as the maximum segment sum problem, is the task of finding a contiguous subarray with the largest sum, within a given one-dimensional array A[1...n] of numbers. It can be solved in O ( n ) {\displaystyle O(n)} time and O ( 1 ) {\displaystyle O(1)} space. Formally, the task is to find indices i {\displaystyle i} and j {\displaystyle j} with 1 ≤ i ≤ j ≤ n {\displaystyle 1\leq i\leq j\leq n} , such that the sum
∑ x = i j A [ x ] {\displaystyle \sum _{x=i}^{j}A[x]}
is as large as possible. (Some formulations of the problem also allow the empty subarray to be considered; by convention, the sum of all values of the empty subarray is zero.) Each number in the input array A could be positive, negative, or zero. For example, for the array of values [−2, 1, −3, 4, −1, 2, 1, −5, 4], the contiguous subarray with the largest sum is [4, −1, 2, 1], with sum 6. Some properties of this problem are:
If the array contains all non-negative numbers, then the problem is trivial; a maximum subarray is the entire array. If the array contains all non-positive numbers, then a solution is any subarray of size 1 containing the maximal value of the array (or the empty subarray, if it is permitted). Several different sub-arrays may have the same maximum sum. Although this problem can be solved using several different algorithmic techniques, including brute force, divide and conquer, dynamic programming, and reduction to shortest paths, a simple single-pass algorithm known as Kadane's algorithm solves it efficiently.
History The maximum subarray problem was proposed by Ulf Grenander in 1977 as a simplified model for maximum likelihood estimation of patterns in digitized images. Grenander was looking to find a rectangular subarray with maximum sum, in a two-dimensional array of real numbers. A brute-force algorithm for the two-dimensional problem runs in O(n6) time; because this was prohibitively slow, Grenander proposed the one-dimensional problem to gain insight into its structure. Grenander derived an algorithm that solves the one-dimensional problem in O(n2) time using prefix sums, improving the brute force running time of O(n3). When Michael Shamos heard about the problem, he overnight devised an O(n log n) divide-and-conquer algorithm for it. Soon after, Shamos described the one-dimensional problem and its history at a Carnegie Mellon University seminar attended by Jay Kadane, who designed within a minute an O(n)-time algorithm, which is as fast as possible. In 1982, David Gries obtained the same O(n)-time algorithm by applying Dijkstra's "standard strategy"; in 1989, Richard Bird derived it by purely algebraic manipulation of the brute-force algorithm using the Bird–Meertens formalism. Grenander's two-dimensional generalization can be solved in O(n3) time either by using Kadane's algorithm as a subroutine, or through a divide-and-conquer approach. Slightly faster algorithms based on distance matrix multiplication have been proposed by Tamaki & Tokuyama (1998) and by Takaoka (2002). There is some evidence that no significantly faster algorithm exists; an algorithm that solves the two-dimensional maximum subarray problem in O(n3−ε) time, for any ε>0, would imply a similarly fast algorithm for the all-pairs shortest paths problem.
Applications Maximum subarray problems arise in many fields, such as genomic sequence analysis and computer vision. Genomic sequence analysis employs maximum subarray algorithms to identify important biological segments of protein sequences that have unusual properties, by assigning scores to points within the sequence that are positive when a motif to be recognized is present, and negative when it is not, and then seeking the maximum subarray among these scores. These problems include conserved segments, GC-rich regions, tandem repeats, low-complexity filter, DNA binding domains, and regions of high charge. In computer vision, bitmap images generally consist only of positive values, for which the maximum subarray problem is trivial: the result is always the whole array. However, after subtracting a threshold value (such as the average pixel value) from each pixel, so that above-average pixels will be positive and below-average pixels will be negative, the maximum subarray problem can be applied to the modified image to detect bright areas within it.
Kadane's algorithm
No empty subarrays admitted Kadane's algorithm scans the given array A [ 1 … n ] {\displaystyle A[1\ldots n]} from left to right. In the j {\displaystyle j} th step, it computes the subarray with the largest sum ending at j {\displaystyle j} ; this sum is maintained in variable current_sum. Moreover, it computes the subarray with the largest sum anywhere in A [ 1 … j ] {\displaystyle A[1\ldots j]} , maintained in variable best_sum, and easily obtained as the maximum of all values of current_sum seen so far, cf. line 7 of the algorithm. As a loop invariant, in the j {\displaystyle j} th step, the old value of current_sum holds the maximum over all i ∈ { 1 , … , j − 1 } {\displaystyle i\in \{1,\ldots ,j-1\}} of the sum A [ i ] + ⋯ + A [ j − 1 ] {\displaystyle A[i]+\cdots +A[j-1]} . Therefore, current_sum + A [ j ] {\displaystyle +A[j]}
… excerpt ends here. Continue reading the full article.

![Maximum subarray problem: Visualization of how sub-arrays change based on start and end positions of a sub-array. Each colored line corresponds to a fixed starting index in the array. The leftmost point of a line represents the single-element sub-array starting at that index, and each subsequent point extends the sub-array by one element to the right. The x-coordinate of a point is the index of the last element in the sub-array, and the y-coordinate is the sum of the sub-array. In this case, the original array from which sub-arrays are taken is [2, 3, -1, -20, 5, 10].](https://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Maximum_Subarray_Visualization.svg/330px-Maximum_Subarray_Visualization.svg.png?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail)

