Array
Maximum Sum
Disjoint Sequence
Algorithm
Data Structures

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.

Practice algorithms

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:

  1. 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])`.
  2. 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: dp[i]=max(dp[i1],array[i]+(dp[i2] if i>=2 else 0))dp[i] = max(dp[i-1], array[i] + (dp[i-2] \text{ if } i >= 2 \text{ else } 0)) • 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]`.
  3. 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, O(n)O(n), where nn is the length of the array. This is efficient for large inputs.

Space Complexity: Can be reduced to O(1)O(1) 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 PointDescription
ObjectiveMaximize the sum of non-contiguous elements in an array.
ApproachUse dynamic programming to iteratively store maximum sums considering disjoint selections.
Time ComplexityO(n)O(n), linear time complexity.
Space ComplexityO(n)O(n) reduced to O(1)O(1) with optimization.
ApplicabilityLarge 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.