




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Problem:
Find
max
h^ 1
`
a [ i
where
a [
n
(^)
is
an
a rra
y
of
integers
(p
ossibly
negative)
and
n
Solution
summax
for
from
to
n
for
h
from
to
n
f
sum
for
i
from
l
to
h
sum
sum
a [ i ];
if
summax
sum
then
summax
sum
g
c 1 + n 1
c 2
n
`
c 3 + h 1
`
c 4
= c 1 + n 1
=
c 2
n
`
( c 3 + c 4 ( h `
= c 1 + n 1
=
c 2
c 3
c 4 (^) )
( n ` )
= c 1 + c 2 n + n 1
=
c 3 + c 4 ) ( n ` )
=
c 4 ( n ` ) 2
= c 1 + c 2 n + n
=
c 3 + c 4 ) `
n
c 4
` 2
= c 1 + c 2 n +
c 3 + c 4
n
=
c 4
n
=
2
= c 1 + c 2 n +
c 3 + c 4
2 n ( n +
c 4
2 n ( n +
n
n
3 )
Problem:
Find
max
h^ 1
`
a [ i
where
a [
n
(^)
is
an
a rra
y
of
integers
(p
ossibly
negative)
and
n
Solution
summax
for
from
to
n
f
sum
for
h
from
to
n
f
sum
sum
a [ h
if
summax
sum
then
summax
sum
g
g
Complexit
y:
c 1 + n 1
c 2
n
`
c 3 ( n ` ) = c 1 + c 2 n + c 3 n
2 c 1 + c 2 n + c 3 ( n +
n
n
2 )
Problem:
Find
max
h^ 1
`
a [ i
where
a [
n
(^)
is
an
a rra
y
of
integers
(p
ossibly
negative)
and
n
Solution
(divide
and
conquer):
Either
the
maximum
of
the
left
half,
or
the
maximum
of
the
right
half,
or
the
max.
of
the
left
half
touching
the
center
the
max.
of
the
right
half
touching
the
center
metho
(^) d:
split
the
p roblem
into
subp
roblems
of
roughly
equal
sizes,
solve
each
subp
roblem
recursively
do
an
extra
small
amount
of
w
o rk
to
combine
the
solutions
of
the
subp
roblems
into
a
solution
of
the
p roblem.
maxsub
p;
q (^) )
=def
if
p
q
then
f
if
a [ p ] <
then
else
a [ p ]
g
else
f = p + 2 q =
k
b ( p + q ) = 2 c ;
lmax
maxsub
p;
k );
rmax
maxsub
k (^) ; q (^) );
lb
dmax
lb
d
for
i
from
k
to
p
f
lb
d
lb
d
a [ i ];
if
lb
dmax
lb
d
then
lb
dmax
lb
d (^) ;
g
rb
dmax
rb
d
for
i
from
k
to
p
f
rb
d
rb
d
a [ i ];
if
rb
dmax
rb
d
then
rb
dmax
rb
d (^) ;
g
return
max
lmax
rmax
lb
dmax
rb
dmax
g
Problem:
Find
max
h^ 1
`
a [ i
where
a [
n
(^)
is
an
a rra
y
of
integers
(p
ossibly
negative)
and
n
Solution
summax
sum
for
j
from
to
n
f
sum
sum
a [ j (^) ];
if
summax
sum
then
summax
sum
else
if
sum
then
sum
g
Complexit
y:
n
)
t 1 (^) ( n ) = 1 t 1
n
linea
r
t 2 (^) ( n ) = 2 t 2
n
exp
onential
t 2
gro
ws
faster
t 1 (^) ( n
)
=
n
t 2 (^) ( n
)
=
n
t 2
gro
ws
faster
t 1 (^) ( n ) = 2 1
n
t 2 (^) ( n ) = 2 2
n y^
t 2
gro
ws
faster
t 1 (^) ( n ) = ( n +
t 2 (^) ( n ) = ( n +
t 2
gro
ws
faster
the
nal
v
alue
the
nal
v
alue