<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
maximum	B-Language
sum	I-Language
subarray	I-Language
problem	I-Language
,	O
also	O
known	O
as	O
the	O
maximum	B-Language
segment	I-Language
sum	I-Language
problem	I-Language
,	O
is	O
the	O
task	O
of	O
finding	O
a	O
contiguous	O
subarray	O
with	O
the	O
largest	O
sum	O
,	O
within	O
a	O
given	O
one-dimensional	O
array	B-Data_Structure
1	O
...	O
n	O
of	O
numbers	O
.	O
</s>
<s>
Each	O
number	O
in	O
the	O
input	O
array	B-Data_Structure
A	O
could	O
be	O
positive	O
,	O
negative	O
,	O
or	O
zero	O
.	O
</s>
<s>
For	O
example	O
,	O
for	O
the	O
array	B-Data_Structure
of	O
values	O
[	O
2	O
,	O
1	O
,	O
3	O
,	O
4	O
,	O
1	O
,	O
2	O
,	O
1	O
,	O
5	O
,	O
4 ]	O
,	O
the	O
contiguous	O
subarray	O
with	O
the	O
largest	O
sum	O
is	O
[	O
4	O
,	O
1	O
,	O
2	O
,	O
1 ]	O
,	O
with	O
sum	O
6	O
.	O
</s>
<s>
If	O
the	O
array	B-Data_Structure
contains	O
all	O
non-negative	O
numbers	O
,	O
then	O
the	O
problem	O
is	O
trivial	O
;	O
a	O
maximum	B-Language
subarray	I-Language
is	O
the	O
entire	O
array	B-Data_Structure
.	O
</s>
<s>
If	O
the	O
array	B-Data_Structure
contains	O
all	O
non-positive	O
numbers	O
,	O
then	O
a	O
solution	O
is	O
any	O
subarray	O
of	O
size	O
1	O
containing	O
the	O
maximal	O
value	O
of	O
the	O
array	B-Data_Structure
(	O
or	O
the	O
empty	O
subarray	O
,	O
if	O
it	O
is	O
permitted	O
)	O
.	O
</s>
<s>
Although	O
this	O
problem	O
can	O
be	O
solved	O
using	O
several	O
different	O
algorithmic	O
techniques	O
,	O
including	O
brute	O
force	O
,	O
divide	B-Algorithm
and	I-Algorithm
conquer	I-Algorithm
,	O
dynamic	B-Algorithm
programming	I-Algorithm
,	O
and	O
reduction	O
to	O
shortest	O
paths	O
,	O
a	O
simple	O
single-pass	O
algorithm	O
known	O
as	O
Kadane	B-Language
's	I-Language
algorithm	I-Language
solves	O
it	O
efficiently	O
.	O
</s>
<s>
The	O
maximum	B-Language
subarray	I-Language
problem	I-Language
was	O
proposed	O
by	O
Ulf	O
Grenander	O
in	O
1977	O
as	O
a	O
simplified	O
model	O
for	O
maximum	O
likelihood	O
estimation	O
of	O
patterns	O
in	O
digitized	O
images	O
.	O
</s>
<s>
Grenander	O
was	O
looking	O
to	O
find	O
a	O
rectangular	O
subarray	O
with	O
maximum	O
sum	O
,	O
in	O
a	O
two-dimensional	O
array	B-Data_Structure
of	O
real	O
numbers	O
.	O
</s>
<s>
When	O
Michael	O
Shamos	O
heard	O
about	O
the	O
problem	O
,	O
he	O
overnight	O
devised	O
an	O
O(n log n )	O
divide-and-conquer	B-Algorithm
algorithm	I-Algorithm
for	O
it	O
.	O
</s>
<s>
In	O
1982	O
,	O
David	O
Gries	O
obtained	O
the	O
same	O
O(n )	O
-time	O
algorithm	O
by	O
applying	O
Dijkstra	O
's	O
"	O
standard	O
strategy	O
"	O
;	O
in	O
1989	O
,	O
Richard	O
Bird	O
derived	O
it	O
by	O
purely	O
algebraic	O
manipulation	O
of	O
the	O
brute-force	O
algorithm	O
using	O
the	O
Bird	B-Application
–	I-Application
Meertens	I-Application
formalism	I-Application
.	O
</s>
<s>
Grenander	O
's	O
two-dimensional	O
generalization	O
can	O
be	O
solved	O
in	O
O(n3 )	O
time	O
either	O
by	O
using	O
Kadane	B-Language
's	I-Language
algorithm	I-Language
as	O
a	O
subroutine	O
,	O
or	O
through	O
a	O
divide-and-conquer	O
approach	O
.	O
</s>
<s>
There	O
is	O
some	O
evidence	O
that	O
no	O
significantly	O
faster	O
algorithm	O
exists	O
;	O
an	O
algorithm	O
that	O
solves	O
the	O
two-dimensional	O
maximum	B-Language
subarray	I-Language
problem	I-Language
in	O
O( n3−ε	O
)	O
time	O
,	O
for	O
any	O
ε>0	O
,	O
would	O
imply	O
a	O
similarly	O
fast	O
algorithm	O
for	O
the	O
all-pairs	O
shortest	O
paths	O
problem	O
.	O
</s>
<s>
Maximum	B-Language
subarray	I-Language
problems	I-Language
arise	O
in	O
many	O
fields	O
,	O
such	O
as	O
genomic	O
sequence	O
analysis	O
and	O
computer	B-Application
vision	I-Application
.	O
</s>
<s>
Genomic	O
sequence	O
analysis	O
employs	O
maximum	B-Language
subarray	I-Language
algorithms	O
to	O
identify	O
important	O
biological	O
segments	O
of	O
protein	O
sequences	O
.	O
</s>
<s>
In	O
computer	B-Application
vision	I-Application
,	O
maximum-subarray	O
algorithms	O
are	O
used	O
on	O
bitmap	O
images	O
to	O
detect	O
the	O
brightest	O
area	O
in	O
an	O
image	O
.	O
</s>
<s>
Example	O
run	O
thumb|500px|Execution	O
of	O
Kadane	B-Language
's	I-Language
algorithm	I-Language
on	O
the	O
above	O
example	O
array	B-Data_Structure
.	O
</s>
<s>
:	O
subarray	O
with	O
largest	O
sum	O
ending	O
at	O
i	O
;	O
:	O
subarray	O
with	O
largest	O
sum	O
encountered	O
so	O
far	O
;	O
a	O
lower	O
case	O
letter	O
indicates	O
an	O
empty	O
array	B-Data_Structure
;	O
variable	O
i	O
is	O
left	O
implicit	O
in	O
Python	B-Language
code	I-Language
.	O
</s>
<s>
It	O
scans	O
the	O
given	O
array	B-Data_Structure
from	O
left	O
to	O
right	O
.	O
</s>
<s>
As	O
a	O
loop	B-Application
invariant	I-Application
,	O
in	O
the	O
th	O
step	O
,	O
the	O
old	O
value	O
of	O
current_sum	O
holds	O
the	O
maximum	O
over	O
all	O
of	O
the	O
sum	O
.	O
</s>
<s>
Thus	O
,	O
the	O
problem	O
can	O
be	O
solved	O
with	O
the	O
following	O
code	O
,	O
expressed	O
here	O
in	O
Python	B-Language
:	O
</s>
<s>
The	O
algorithm	O
can	O
be	O
adapted	O
to	O
the	O
case	O
which	O
disallows	O
empty	O
subarrays	O
or	O
to	O
keep	O
track	O
of	O
the	O
starting	O
and	O
ending	O
indices	O
of	O
the	O
maximum	B-Language
subarray	I-Language
.	O
</s>
<s>
This	O
algorithm	O
calculates	O
the	O
maximum	B-Language
subarray	I-Language
ending	O
at	O
each	O
position	O
from	O
the	O
maximum	B-Language
subarray	I-Language
ending	O
at	O
the	O
previous	O
position	O
,	O
so	O
it	O
can	O
be	O
viewed	O
as	O
a	O
trivial	O
case	O
of	O
dynamic	B-Algorithm
programming	I-Algorithm
.	O
</s>
<s>
For	O
correctness	O
,	O
an	O
exception	O
should	O
be	O
raised	O
when	O
the	O
input	O
array	B-Data_Structure
is	O
empty	O
,	O
since	O
an	O
empty	O
array	B-Data_Structure
has	O
no	O
maximum	O
nonempty	O
subarray	O
.	O
</s>
<s>
If	O
the	O
array	B-Data_Structure
is	O
non-empty	O
,	O
its	O
first	O
element	O
can	O
be	O
used	O
in	O
place	O
of	O
negative	O
infinity	O
,	O
if	O
needed	O
to	O
avoid	O
mixing	O
numeric	O
and	O
non-numeric	O
values	O
.	O
</s>
<s>
The	O
algorithm	O
can	O
be	O
modified	O
to	O
keep	O
track	O
of	O
the	O
starting	O
and	O
ending	O
indices	O
of	O
the	O
maximum	B-Language
subarray	I-Language
as	O
well	O
.	O
</s>
<s>
Because	O
of	O
the	O
way	O
this	O
algorithm	O
uses	O
optimal	O
substructures	O
(	O
the	O
maximum	B-Language
subarray	I-Language
ending	O
at	O
each	O
position	O
is	O
calculated	O
in	O
a	O
simple	O
way	O
from	O
a	O
related	O
but	O
smaller	O
and	O
overlapping	O
subproblem	O
:	O
the	O
maximum	B-Language
subarray	I-Language
ending	O
at	O
the	O
previous	O
position	O
)	O
this	O
algorithm	O
can	O
be	O
viewed	O
as	O
a	O
simple/trivial	O
example	O
of	O
dynamic	B-Algorithm
programming	I-Algorithm
.	O
</s>
<s>
The	O
runtime	O
complexity	O
of	O
Kadane	B-Language
's	I-Language
algorithm	I-Language
is	O
and	O
its	O
space	O
complexity	O
is	O
.	O
</s>
<s>
showed	O
how	O
to	O
find	O
the	O
k	O
largest	O
subarray	O
sums	O
in	O
a	O
one-dimensional	O
array	B-Data_Structure
,	O
in	O
the	O
optimal	O
time	O
bound	O
.	O
</s>
