Maximum Sum Increasing Subsequence (original) (raw)
Last Updated : 02 May, 2025
Given an **array arr[] of n positive integers. The task is to find the **sum of the maximum sum subsequence of the given array such that the integers in the subsequence are sorted in strictly increasing order.
**Examples:
**Input: arr[] = [1, 101, 2, 3, 100]
**Output: 106
**Explanation: The maximum sum of a increasing sequence is obtained from [1, 2, 3, 100].**Input: arr[] = [4, 1, 2, 3]
**Output: 6
**Explanation: The maximum sum of a increasing sequence is obtained from [1, 2, 3].
Table of Content
- [Naive Approach] Using Recursion - O(2^n) Time and O(n) Space
- [Better Approach 1] Using Top-Down DP (Memoization) - O(n^2) Time and O(n) Space
- [Better Approach 2] Using Bottom-Up DP (Tabulation) - O(n^2) Time and O(n) Space
- [Expected Approach] Using Dynamic Programming and Greedy - O(n log(n)) time and O(n) space
[Naive Approach] Using Recursion - O(2^n) Time and O(n) Space
This problem is a variation of the standard Longest Increasing Subsequence (LIS) problem. In this problem, we will consider the **sum of subsequence instead of its length.
The idea is to solve the problem **recursively by considering every **index i in the array as a potential end of an increasing subsequence. For each index i, initialize the **maximum sum subsequence ending at i to arr[i]. Then, for every **previous index j (0 ≤ j < i), check if **arr[j] < arr[i]. If this condition is met, recursively compute the maximum sum subsequence ending at j and update the **result for i by taking the maximum of its current value and arr[i] + maxSum(j). Finally, **return the **maximum value obtained across all indices as the result.
C++ `
// C++ program to find maximum // Sum Increasing Subsequence #include <bits/stdc++.h> using namespace std;
// Recursive function which finds the // maximum sum increasing Subsequence // ending at index i. int maxSumRecur(int i, vector &arr) {
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j= i-1; j>=0; j--) {
if (arr[j]<arr[i]) {
ans = max(ans, arr[i]+maxSumRecur(j, arr));
}
}
return ans;
}
int maxSumIS(vector &arr) { int ans = 0;
// Compute maximum sum for each
// index and compare it with ans.
for (int i=0; i<arr.size(); i++) {
ans = max(ans, maxSumRecur(i, arr));
}
return ans;
}
int main() { vector arr = {1, 101, 2, 3, 100}; cout << maxSumIS(arr);
return 0;
}
Java
// Java program to find maximum // Sum Increasing Subsequence
class GfG {
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
static int maxSumRecur(int i, int[] arr) {
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.max(ans, arr[i] + maxSumRecur(j, arr));
}
}
return ans;
}
static int maxSumIS(int[] arr) {
int ans = 0;
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.length; i++) {
ans = Math.max(ans, maxSumRecur(i, arr));
}
return ans;
}
public static void main(String[] args) {
int[] arr = {1, 101, 2, 3, 100};
System.out.println(maxSumIS(arr));
}
}
Python
Python program to find maximum
Sum Increasing Subsequence
Recursive function which finds the
maximum sum increasing Subsequence
ending at index i.
def maxSumRecur(i, arr): ans = arr[i]
# Compute values for all
# j in range (0, i-1)
for j in range(i - 1, -1, -1):
if arr[j] < arr[i]:
ans = max(ans, arr[i] + maxSumRecur(j, arr))
return ans
def maxSumIS(arr): ans = 0
# Compute maximum sum for each
# index and compare it with ans.
for i in range(len(arr)):
ans = max(ans, maxSumRecur(i, arr))
return ans
if name == "main": arr = [1, 101, 2, 3, 100] print(maxSumIS(arr))
C#
// C# program to find maximum // Sum Increasing Subsequence
using System;
class GfG {
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
static int maxSumRecur(int i, int[] arr) {
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.Max(ans, arr[i] + maxSumRecur(j, arr));
}
}
return ans;
}
static int maxSumIS(int[] arr) {
int ans = 0;
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.Length; i++) {
ans = Math.Max(ans, maxSumRecur(i, arr));
}
return ans;
}
static void Main(string[] args) {
int[] arr = {1, 101, 2, 3, 100};
Console.WriteLine(maxSumIS(arr));
}
}
JavaScript
// JavaScript program to find maximum // Sum Increasing Subsequence
// Recursive function which finds the // maximum sum increasing Subsequence // ending at index i. function maxSumRecur(i, arr) { let ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (let j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.max(ans, arr[i] + maxSumRecur(j, arr));
}
}
return ans;
}
function maxSumIS(arr) { let ans = 0;
// Compute maximum sum for each
// index and compare it with ans.
for (let i = 0; i < arr.length; i++) {
ans = Math.max(ans, maxSumRecur(i, arr));
}
return ans;
}
const arr = [1, 101, 2, 3, 100]; console.log(maxSumIS(arr));
`
[Better Approach 1] Using Top-Down DP (Memoization) - O(n^2) Time and O(n) Space
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
**1. Optimal Substructure: The maximum sum increasing subsequence ending at index **i, i.e., **maxSum(i), depends on the optimal solutions of the subproblems **maxSum(j) where **0 <= j < i and **arr[j]<arr[i]. By finding the **maximum value in these optimal substructures, we can efficiently calculate the maximum sum increasing subsequence at index i.
**2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times.
- There is **one parameter, **i that changes in the **recursive solution. So we create a **1D array of size n for memoization.
- We initialize this array as -1 to indicate nothing is computed initially.
- Now we modify our recursive solution to first check if the value is -1, then only make recursive calls. This way, we avoid re-computations of the same subproblems. C++ `
// C++ program to find maximum // Sum Increasing Subsequence #include <bits/stdc++.h> using namespace std;
// Recursive function which finds the // maximum sum increasing Subsequence // ending at index i. int maxSumRecur(int i, vector &arr, vector &memo) {
// If value is computed,
// return it.
if (memo[i]!=-1) return memo[i];
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i-1; j >= 0; j--) {
if (arr[j]<arr[i]) {
ans = max(ans, arr[i]+maxSumRecur(j, arr, memo));
}
}
return memo[i] = ans;
}
int maxSumIS(vector &arr) { int ans = 0;
vector<int> memo(arr.size(), -1);
// Compute maximum sum for each
// index and compare it with ans.
for (int i=0; i<arr.size(); i++) {
ans = max(ans, maxSumRecur(i, arr, memo));
}
return ans;
}
int main() { vector arr = {1, 101, 2, 3, 100}; cout << maxSumIS(arr);
return 0;
}
Java
// Java program to find maximum // Sum Increasing Subsequence
import java.util.Arrays;
class GfG {
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
static int maxSumRecur(int i, int[] arr, int[] memo) {
// If value is computed,
// return it.
if (memo[i] != -1) return memo[i];
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.max(ans, arr[i] + maxSumRecur(j, arr, memo));
}
}
return memo[i] = ans;
}
static int maxSumIS(int[] arr) {
int ans = 0;
int[] memo = new int[arr.length];
Arrays.fill(memo, -1);
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.length; i++) {
ans = Math.max(ans, maxSumRecur(i, arr, memo));
}
return ans;
}
public static void main(String[] args) {
int[] arr = {1, 101, 2, 3, 100};
System.out.println(maxSumIS(arr));
}
}
Python
Python program to find maximum
Sum Increasing Subsequence
Recursive function which finds the
maximum sum increasing Subsequence
ending at index i.
def maxSumRecur(i, arr, memo):
# If value is computed,
# return it.
if memo[i] != -1:
return memo[i]
ans = arr[i]
# Compute values for all
# j in range (0, i-1)
for j in range(i - 1, -1, -1):
if arr[j] < arr[i]:
ans = max(ans, arr[i] + maxSumRecur(j, arr, memo))
memo[i] = ans
return ans
def maxSumIS(arr): ans = 0 memo = [-1] * len(arr)
# Compute maximum sum for each
# index and compare it with ans.
for i in range(len(arr)):
ans = max(ans, maxSumRecur(i, arr, memo))
return ans
if name == "main": arr = [1, 101, 2, 3, 100] print(maxSumIS(arr))
C#
// C# program to find maximum // Sum Increasing Subsequence
using System;
class GfG {
// Recursive function which finds the
// maximum sum increasing Subsequence
// ending at index i.
static int maxSumRecur(int i, int[] arr, int[] memo) {
// If value is computed,
// return it.
if (memo[i] != -1) return memo[i];
int ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (int j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.Max(ans, arr[i] + maxSumRecur(j, arr, memo));
}
}
return memo[i] = ans;
}
static int maxSumIS(int[] arr) {
int ans = 0;
int[] memo = new int[arr.Length];
Array.Fill(memo, -1);
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.Length; i++) {
ans = Math.Max(ans, maxSumRecur(i, arr, memo));
}
return ans;
}
static void Main(string[] args) {
int[] arr = {1, 101, 2, 3, 100};
Console.WriteLine(maxSumIS(arr));
}
}
JavaScript
// JavaScript program to find maximum // Sum Increasing Subsequence
// Recursive function which finds the // maximum sum increasing Subsequence // ending at index i. function maxSumRecur(i, arr, memo) {
// If value is computed,
// return it.
if (memo[i] !== -1) return memo[i];
let ans = arr[i];
// Compute values for all
// j in range (0, i-1)
for (let j = i - 1; j >= 0; j--) {
if (arr[j] < arr[i]) {
ans = Math.max(ans, arr[i] + maxSumRecur(j, arr, memo));
}
}
memo[i] = ans;
return ans;
}
function maxSumIS(arr) { let ans = 0; let memo = Array(arr.length).fill(-1);
// Compute maximum sum for each
// index and compare it with ans.
for (let i = 0; i < arr.length; i++) {
ans = Math.max(ans, maxSumRecur(i, arr, memo));
}
return ans;
}
const arr = [1, 101, 2, 3, 100]; console.log(maxSumIS(arr));
`
[Better Approach 2] Using Bottom-Up DP (Tabulation) - O(n^2) Time and O(n) Space
The approach is similar to the **previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner. The idea is to create a 1-D array. Then fill the values using **dp[i] = max(arr[i] + dp[j]) for **0 <= j < i and arr[j]<arr[i].
C++ `
// C++ program to find maximum // Sum Increasing Subsequence #include <bits/stdc++.h> using namespace std;
int maxSumIS(vector &arr) { int ans = 0;
vector<int> dp(arr.size());
// Compute maximum sum for each
// index and compare it with ans.
for (int i=0; i<arr.size(); i++) {
dp[i] = arr[i];
for (int j=0; j<i; j++) {
if (arr[j]<arr[i]) {
dp[i] = max(dp[i], arr[i]+dp[j]);
}
}
ans = max(ans, dp[i]);
}
return ans;
}
int main() { vector arr = {1, 101, 2, 3, 100}; cout << maxSumIS(arr);
return 0;
}
Java
// Java program to find maximum // Sum Increasing Subsequence
import java.util.Arrays;
class GfG {
static int maxSumIS(int[] arr) {
int ans = 0;
int[] dp = new int[arr.length];
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.length; i++) {
dp[i] = arr[i];
for (int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = Math.max(dp[i], arr[i] + dp[j]);
}
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
public static void main(String[] args) {
int[] arr = {1, 101, 2, 3, 100};
System.out.println(maxSumIS(arr));
}
}
Python
Python program to find maximum
Sum Increasing Subsequence
def maxSumIS(arr): ans = 0
dp = [0] * len(arr)
# Compute maximum sum for each
# index and compare it with ans.
for i in range(len(arr)):
dp[i] = arr[i]
for j in range(i):
if arr[j] < arr[i]:
dp[i] = max(dp[i], arr[i] + dp[j])
ans = max(ans, dp[i])
return ans
if name == "main": arr = [1, 101, 2, 3, 100] print(maxSumIS(arr))
C#
// C# program to find maximum // Sum Increasing Subsequence
using System;
class GfG {
static int maxSumIS(int[] arr) {
int ans = 0;
int[] dp = new int[arr.Length];
// Compute maximum sum for each
// index and compare it with ans.
for (int i = 0; i < arr.Length; i++) {
dp[i] = arr[i];
for (int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = Math.Max(dp[i], arr[i] + dp[j]);
}
}
ans = Math.Max(ans, dp[i]);
}
return ans;
}
static void Main(string[] args) {
int[] arr = {1, 101, 2, 3, 100};
Console.WriteLine(maxSumIS(arr));
}
}
JavaScript
// JavaScript program to find maximum // Sum Increasing Subsequence
function maxSumIS(arr) { let ans = 0;
let dp = new Array(arr.length);
// Compute maximum sum for each
// index and compare it with ans.
for (let i = 0; i < arr.length; i++) {
dp[i] = arr[i];
for (let j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = Math.max(dp[i], arr[i] + dp[j]);
}
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
const arr = [1, 101, 2, 3, 100]; console.log(maxSumIS(arr));
`
[Expected Approach] Using Dynamic Programming and Greedy - O(n log(n)) time and O(n) space
This approach combines **dynamic programming and a **greedy strategy to efficiently find the maximum sum of an increasing subsequence in an array. The key idea is based on **Longest Increasing Subsequence (or LIS) using Binary Search. Unlike LIS, here we need to compare sums, so we cannot do binary search over the array. We use a Tree Map or Similar Structure to keep sums corresponding to different values. We build the solution incrementally and prune invalid subsequences to ensure that only the most promising candidates are kept in the tree map.
- **Start from the End:
- Traverse the array from the last element to the first.
- For each element, consider the best subsequences that can come after it.
- **Dynamic Programming with Greedy Strategy:
- Use dynamic programming to build the solution incrementally.
- For each element, calculate the best sum of an increasing subsequence that can start from it.
- **Store Maximum Subsequence Sum:
- Use a **map (or similar structure) to store the maximum sum of subsequences for each element.
- The key is the element, and the value is the maximum sum possible for subsequences starting from that element.
- **Greedy Pruning:
- As you traverse the array, discard subsequences that cannot contribute to the best result.
- Remove any subsequences whose sum is less than the current element's potential sum.
- **Maintain Increasing Order:
- Ensure subsequences are strictly increasing by only considering elements greater than the current element for the next subsequence.
- **Efficient Updates and Lookups:
- The map allows quick lookups to find the best subsequences for the next element.
- Update the map as you encounter better subsequences that include the current element. C++ `
#include #include #include using namespace std;
int maxSumIS(vector &arr) { int n = (int)arr.size();
// Map to store the maximum subsequence sum for each element
map<int, int> mp;
int answer = 0;
// Traverse the array from end to start
for (int i = n - 1; i >= 0; i--)
{
int val = arr[i];
auto it = mp.upper_bound(val);
int bestNext = 0;
if (it != mp.end())
{
bestNext = it->second;
}
int temp = val + bestNext;
answer = max(answer, temp);
// Find and remove all subsequences
// that cannot contribute to the best result
auto removeIt = mp.lower_bound(val);
if (removeIt != mp.begin())
{
auto jt = removeIt;
--jt;
while (true)
{
// Prune invalid subsequences by checking
// if they cannot contribute to a better result
if (jt->first < val && jt->second <= temp)
{
if (jt == mp.begin())
{
mp.erase(jt);
break;
}
else
{
auto temp = jt;
--jt;
mp.erase(temp);
}
}
else
{
break;
}
}
}
// Insert the current element and its best subsequence sum into the map
auto exist = mp.lower_bound(val);
if (exist == mp.end() || exist->first != val)
{
// If element doesn't exist, insert it
mp.insert({val, temp});
}
else
{
if (exist->second < temp)
{
// If a better subsequence sum exists, update it
exist->second = temp;
}
}
}
return answer;
}
int main() { vector arr = {1, 101, 2, 3, 100}; cout << maxSumIS(arr) << "\n";
return 0;
}
Java
import java.util.*;
public class GfG{ public static int maxSumIS(int[] arr) { int n = arr.length;
// Map to store the maximum subsequence sum for each
// element
TreeMap<Integer, Integer> mp = new TreeMap<>();
int answer = 0;
// Traverse the array from end to start
for (int i = n - 1; i >= 0; i--) {
int val = arr[i];
Integer bestNext = mp.higherKey(val);
int bestValue
= (bestNext != null) ? mp.get(bestNext) : 0;
int temp = val + bestValue;
answer = Math.max(answer, temp);
// Find and remove all subsequences
// that cannot contribute to the best result
Iterator<Map.Entry<Integer, Integer> > removeIt
= mp.tailMap(val, false)
.entrySet()
.iterator();
while (removeIt.hasNext()) {
Map.Entry<Integer, Integer> entry
= removeIt.next();
if (entry.getValue() <= temp) {
removeIt.remove();
}
else {
break;
}
}
// Insert the current element and its best
// subsequence sum into the map
if (!mp.containsKey(val)) {
mp.put(val, temp);
}
else {
if (mp.get(val) < temp) {
mp.put(val, temp);
}
}
}
return answer;
}
public static void main(String[] args)
{
int[] arr = { 1, 101, 2, 3, 100 };
System.out.println(maxSumIS(arr));
}
}
Python
def max_sum_is(arr): n = len(arr)
# Map to store the maximum subsequence sum for each element
mp = {}
answer = 0
# Traverse the array from end to start
for i in range(n - 1, -1, -1):
val = arr[i]
best_next = max((k for k in mp.keys() if k > val), default=None)
best_value = mp[best_next] if best_next is not None else 0
temp = val + best_value
answer = max(answer, temp)
# Find and remove all subsequences
# that cannot contribute to the best result
keys_to_remove = [k for k in mp if k >= val]
for k in keys_to_remove:
if mp[k] <= temp:
del mp[k]
else:
break
# Insert the current element and its best subsequence sum into the map
if val not in mp:
mp[val] = temp
else:
if mp[val] < temp:
mp[val] = temp
return answer
arr = [1, 101, 2, 3, 100] print(max_sum_is(arr))
C#
using System; using System.Collections.Generic;
class GfG{ public static int MaxSumIS(int[] arr) { int n = arr.Length;
// Map to store the maximum subsequence sum for each
// element
SortedDictionary<int, int> mp
= new SortedDictionary<int, int>();
int answer = 0;
// Traverse the array from end to start
for (int i = n - 1; i >= 0; i--) {
int val = arr[i];
int bestNext = 0;
foreach(var key in mp.Keys)
{
if (key > val) {
bestNext = mp[key];
break;
}
}
int temp = val + bestNext;
answer = Math.Max(answer, temp);
// Find and remove all subsequences
// that cannot contribute to the best result
var keysToRemove = new List<int>();
foreach(var key in mp.Keys)
{
if (key >= val)
break;
if (mp[key] <= temp) {
keysToRemove.Add(key);
}
else {
break;
}
}
foreach(var key in keysToRemove)
{
mp.Remove(key);
}
// Insert the current element and its best
// subsequence sum into the map
if (!mp.ContainsKey(val)) {
mp[val] = temp;
}
else {
if (mp[val] < temp) {
mp[val] = temp;
}
}
}
return answer;
}
public static void Main()
{
int[] arr = { 1, 101, 2, 3, 100 };
Console.WriteLine(MaxSumIS(arr));
}
}
JavaScript
function maxSumIS(arr) { let n = arr.length;
// Map to store the maximum subsequence sum for each
// element
let mp = new Map();
let answer = 0;
// Traverse the array from end to start
for (let i = n - 1; i >= 0; i--) {
let val = arr[i];
let bestNext = 0;
for (let [key, value] of mp) {
if (key > val) {
bestNext = value;
break;
}
}
let temp = val + bestNext;
answer = Math.max(answer, temp);
// Find and remove all subsequences
// that cannot contribute to the best result
for (let [key, value] of mp) {
if (key >= val)
break;
if (value <= temp) {
mp.delete(key);
}
else {
break;
}
}
// Insert the current element and its best
// subsequence sum into the map
if (!mp.has(val)) {
mp.set(val, temp);
}
else {
if (mp.get(val) < temp) {
mp.set(val, temp);
}
}
}
return answer;
}
let arr = [ 1, 101, 2, 3, 100 ]; console.log(maxSumIS(arr));
`