How can I find the maximum sum of a sub-sequence using dynamic programming?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This problem is often asked with slightly inconsistent wording. If "sub-sequence" really means a contiguous slice of the array, the classic dynamic-programming answer is Kadane’s algorithm. If it means non-contiguous, the answer is different and much simpler.
Clarify the Problem First
For an array such as [4, -1, 2, 1, -5], there are two common interpretations:
- Contiguous subsequence, more commonly called a subarray.
- Non-contiguous subsequence, where you may skip elements but preserve order.
Most interview questions that ask for a dynamic-programming recurrence mean the contiguous version. That is the version discussed first here.
Dynamic Programming for the Contiguous Case
Let dp[i] be the maximum sum of a contiguous subsequence that ends exactly at index i. At each position, you either start fresh from the current element or extend the best subsequence ending at the previous index.
That gives the recurrence:
- '
dp[i] = max(arr[i], arr[i] + dp[i - 1])'
The global answer is the maximum value among all dp[i] values.
This is Kadane’s algorithm. It is dynamic programming because each step reuses the solution to the previous subproblem.
Recovering the Actual Subsequence
If you also need the indices, track where the current run started and when a new best range appears.
That is useful when the task is not only to compute the score but also to show which elements produced it.
If the Problem Is Truly Non-Contiguous
For a non-contiguous maximum-sum subsequence, the rule changes. You take every positive value, because skipping a positive number would only make the sum smaller. If all numbers are negative, the best answer is the largest single element.
This difference is why the problem statement matters so much. The contiguous answer is 6, while the non-contiguous answer is 11 for the same input.
Complexity
The contiguous dynamic-programming solution runs in linear time and constant extra space when you keep only the previous state. That is one reason it appears so often in interviews and competitive programming.
Common Pitfalls
- Solving the contiguous problem when the question really allows non-contiguous choices gives the wrong answer.
- Initializing the running sum to zero breaks the all-negative case.
- Building an entire
dparray is unnecessary unless you need to inspect intermediate states. - Forgetting to track indices makes it impossible to reconstruct the winning subsequence later.
- Using the word subsequence without clarifying the definition causes avoidable confusion.
Summary
- Most dynamic-programming versions of this problem mean the maximum-sum contiguous subsequence.
- Kadane’s algorithm keeps the best sum ending at the current index and updates a global best.
- The contiguous solution runs in linear time with constant extra space.
- If the problem is truly non-contiguous, sum the positive values or take the maximum element when all are negative.
- Always confirm which definition of subsequence the question intends.

