Problem statement
Given an array input[] of size ‘N’ with all the integer elements, the task is to find the maximum sum of an increasing subsequence in the array.
Example 1
Input
Input[] = { 4, 6, 2, 5, 8 }
N = 5
Output
18
Explanation
All the increasing subsequences are
4, 6, 8
2, 5, 8
Out of both the increasing subsequences, the maximum sum will be generated by the first subsequence, which is 4 + 6 + 8 = 18, hence the Maximum Sum of Increasing Subsequence in the given input array is 18.
Approach
The approach for this problem is to use Dynamic Programming by creating a DP table to store the result, and the value present at DP[i][j] will depict the maximum sum of any increasing subsequence of length ‘j’ ending at ith index. Any subsequence of length ‘j - 1’ and ending at any of the indexes before the current index can be further extended to the current index by adding the current element’s value to it, and the size will now get changed to j. For any index ‘i’, DP[i][L] will depict the maximum sum that a subsequence of size ‘L’ ending at this index can have. We can get our result or answer by finding the maximum value amongst all DP[i][L] but this approach will cost quadratic time complexity.
We can optimize the solution using the Fenwick tree. The idea to solve this problem is to use the map and Fenwick tree. Make a map containing unique occurring elements and their index value, like for an array { 4, 6, 2, 5, 8 }
4 will be assigned index 0
6 will be assigned index 1
2 will be assigned index 2
5 will be assigned index 3
8 will be assigned index 4
Then using the map, construct Fenwick tree to perform the basic operations. Given below is the detailed algorithm and implementation for the same.
Also read, Euclid GCD Algorithm




