0% found this document useful (0 votes)
97 views2 pages

Maximum Subsequence Sum: 2.1 Naive Implementation

This document describes algorithms for finding the maximum subsequence sum in an array of integers, including both naive and improved approaches. A naive implementation sums every possible subsequence in O(n3) time. An improved version eliminates the innermost loop to achieve O(n2) time by keeping a running sum within the j-loop. The best approach uses a linear time O(n) algorithm that tracks a running sum s which resets to 0 whenever negative, holding the maximum sum of all subarrays ending at index i, with m tracking the overall maximum.

Uploaded by

Andrew Lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views2 pages

Maximum Subsequence Sum: 2.1 Naive Implementation

This document describes algorithms for finding the maximum subsequence sum in an array of integers, including both naive and improved approaches. A naive implementation sums every possible subsequence in O(n3) time. An improved version eliminates the innermost loop to achieve O(n2) time by keeping a running sum within the j-loop. The best approach uses a linear time O(n) algorithm that tracks a running sum s which resets to 0 whenever negative, holding the maximum sum of all subarrays ending at index i, with m tracking the overall maximum.

Uploaded by

Andrew Lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Maximum Subsequence Sum

Problem

Input: An array A[1...n] of integers. Negative values are allowed.


Output: The maximum sum among all subsequences in the array, or 0 if
all values are negative.

Algorithm

2.1

Naive Implementation

A naive implementation is to sum every possible subsequence and keep a


running maximum. This approach is O(n3 ).
1

for i in [1 , n ]:
for j in [i , n ]:
for k in [i , j ]:
sum += A [ k ]
max = max ( max , sum )
return max

2.1.1

Runtime

The runtime is upper-bounded by O(n3 ), but it is possible to derive the


constants. Note the changes of variables to simplify the sums.
j
n X
n X
X

1=

i=1 j=i k=i

n X
n
X

(j i + 1)

(1)

i=1 j=i

n ni+1
X
X
i=1

(2)

j=1
n

1X
=
[(n i + 1)(n i + 2)]
2
=

1
2

i=1
n
X

i(i + 1)

(3)
(4)

i=1

1
= n(n + 1)(n + 2)
2

(5)

Maximum Subsequence Sum


2.2

Improved Naive Implementation

We can eliminate the innermost loop by keeping a running sum within the
j-loop. This sum represents the sum of the i-j subarray. This approach runs
in O(n2 ).
2

for i in [1 , n ]:
for j in [i , n ]:
sum += A [ j ]
max = max ( max , sum )
return max

2.3
1

Linear Algorithm

m = s = 0
for i in [1 , n ]:
s = max ( s + A [ i ] , 0)
m = max (m , s )

The key idea for this version of the algorithm is that in s we hold a
running sum that gets reset to 0 whenever it becomes negative, which means
that it holds the maximum sum of all subarrays that end at index i. Then
m holds the maximum subsequence sum subsequence encountered at the
current iteration of the loop.

You might also like