finding maximum sum of a disjoint sequence of an array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Finding the maximum sum of a disjoint sequence of an array is a classic problem that arises in computer science and mathematical optimization. This task requires identifying non-overlapping subarrays or subsequences that maximize the total sum.
Problem Description
Given an array of integers, a disjoint subsequence is a sequence of numbers that can be obtained by deleting some elements of the array without reordering the remaining elements. The challenge is to find subsequences whose sum is maximized, ensuring the chosen elements are not contiguous.
Technical Explanation
The solution to this problem can be approached using a dynamic programming algorithm. The idea is to use a running sum to keep track of the maximum sum possible by selecting non-adjacent elements. Here is the step-by-step process of the algorithm:
Steps:
- Initialization: • Create an array `dp` where `dp[i]` stores the maximum sum that can be obtained considering the first `i` elements of the array. • Initialize `dp[0]` with the maximum of zero or the first element of the array, i.e., `dp[0] = max(0, array[0])`.
- Recursive Relation: • For each subsequent element, decide whether to include the current element in the sum or not. Ensure that sequential elements are not included. • Use the formula: • This relation is derived by considering two cases: • Not including the current element: The sum remains as in `dp[i-1]`. • Including the current element: Add `array[i]` to `dp[i-2]`.
- Result: • The result is found in `dp[n-1]`, where `n` is the number of elements in the array.
Example
Consider the array `[3, 2, 7, 10]`:
• Initialize `dp[0] = max(0, 3) = 3`. • For `i = 1`, `dp[1] = max(dp[0], array[1]) = max(3, 2) = 3`. • For `i = 2`, `dp[2] = max(dp[1], array[2] + dp[0]) = max(3, 7 + 3) = 10`. • For `i = 3`, `dp[3] = max(dp[2], array[3] + dp[1]) = max(10, 10 + 3) = 13`.
Thus, the maximum sum of a disjoint subsequence is `13`.
Key Considerations
• Time Complexity: The dynamic programming approach runs in linear time, , where is the length of the array. This is efficient for large inputs.
• Space Complexity: Can be reduced to by storing only two previous results instead of maintaining an entire `dp` array, leveraging the fact that each step depends only on the result of the previous two steps.
Subtopics
Handling Constraints
• Negative Elements: If negative numbers are present, the algorithm inherently skips them in maximizing the sum since any sum that includes them can be improved by excluding them.
• All Negative Array: If the entire array consists of negative numbers, the algorithm will return `0`, representing no subsequence chosen.
Possible Variations
• Weighted Disjoint Subsequences: The general problem can be varied by introducing weights or different conditions for including elements, modifying the dynamic programming relation accordingly.
• Multiple Disjoint Subsequences: Instead of a single maximum sum, the challenge might be to find multiple disjoint subsequences whose combined sum is maximized.
Summary
| Key Point | Description |
| Objective | Maximize the sum of non-contiguous elements in an array. |
| Approach | Use dynamic programming to iteratively store maximum sums considering disjoint selections. |
| Time Complexity | , linear time complexity. |
| Space Complexity | reduced to with optimization. |
| Applicability | Large datasets, efficiently solves even under constraints like negative numbers. |
This problem not only tests algorithmic efficiency but also emphasizes dynamic programming's utility in solving optimization problems. Understanding this approach provides a foundation for tackling more complex scenarios involving constraints on array selections.
Related reading
- Finding mean and median in constant time
- Finding middle element of linked list with 1 pass, is this a creative useless answer?
- Finding Minimum Completion Time of Scheduled Tasks with Topological Sort
- Finding minimum cut-sets between bounded subgraphs
- Finding median of large set of numbers too big to fit into memory
- Finding median of list in Python
- Finding minimum moves required for making 2 strings equal
- Finding minimum number of points which covers entire set of intervals?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.