The Wayback Machine - https://web.archive.org/web/20240905180317/https://www.geeksforgeeks.org/maximum-sum-increasing-subsequence-dp-14/
Open In App

Maximum Sum Increasing Subsequence | DP-14

Last Updated : 03 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Show Topics
Solve Problem
Medium
40.02%
1.9L

Given an array of n positive integers. Write a program to find the sum of maximum sum subsequence of the given array such that the integers in the subsequence are sorted in increasing order. For example, if input is {1, 101, 2, 3, 100, 4, 5}, then output should be 106 (1 + 2 + 3 + 100), if the input array is {3, 4, 5, 10}, then output should be 22 (3 + 4 + 5 + 10) and if the input array is {10, 5, 4, 3}, then output should be 10

Solution: This problem is a variation of the standard Longest Increasing Subsequence (LIS) problem. We need a slight change in the Dynamic Programming solution of LIS problem. All we need to change is to use sum as a criteria instead of a length of increasing subsequence.

Following are the Dynamic Programming solution to the problem :  

C++




/* Dynamic Programming implementation 
of Maximum Sum Increasing Subsequence 
(MSIS) problem */
#include <bits/stdc++.h>
using namespace std;
  
/* maxSumIS() returns the maximum 
sum of increasing subsequence 
in arr[] of size n */
int maxSumIS(int arr[], int n) 
{ 
    int i, j, max = 0; 
    int msis[n]; 
  
    /* Initialize msis values 
    for all indexes */
    for ( i = 0; i < n; i++ ) 
        msis[i] = arr[i]; 
  
    /* Compute maximum sum values 
    in bottom up manner */
    for ( i = 1; i < n; i++ ) 
        for ( j = 0; j < i; j++ ) 
            if (arr[i] > arr[j] && 
                msis[i] < msis[j] + arr[i]) 
                msis[i] = msis[j] + arr[i]; 
  
    /* Pick maximum of 
    all msis values */
    for ( i = 0; i < n; i++ ) 
        if ( max < msis[i] ) 
            max = msis[i]; 
  
    return max; 
} 
  
// Driver Code 
int main() 
{ 
    int arr[] = {1, 101, 2, 3, 100, 4, 5}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    cout << "Sum of maximum sum increasing "
            "subsequence is " << maxSumIS( arr, n ) << endl; 
    return 0; 
} 
  
// This is code is contributed by rathbhupendra


C




/* Dynamic Programming implementation 
of Maximum Sum Increasing Subsequence 
(MSIS) problem */
#include<stdio.h>
  
/* maxSumIS() returns the maximum 
   sum of increasing subsequence
   in arr[] of size n */
int maxSumIS(int arr[], int n)
{
    int i, j, max = 0;
    int msis[n];
  
    /* Initialize msis values
       for all indexes */
    for ( i = 0; i < n; i++ )
        msis[i] = arr[i];
  
    /* Compute maximum sum values 
       in bottom up manner */
    for ( i = 1; i < n; i++ )
        for ( j = 0; j < i; j++ )
            if (arr[i] > arr[j] && 
                msis[i] < msis[j] + arr[i])
                msis[i] = msis[j] + arr[i];
  
    /* Pick maximum of
       all msis values */
    for ( i = 0; i < n; i++ )
        if ( max < msis[i] )
            max = msis[i];
  
    return max;
}
  
// Driver Code
int main()
{
    int arr[] = {1, 101, 2, 3, 100, 4, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    printf("Sum of maximum sum increasing " 
            "subsequence is %d\n",
              maxSumIS( arr, n ) );
    return 0;
}


Java




/* Dynamic Programming Java 
   implementation of Maximum Sum
   Increasing Subsequence (MSIS)
   problem */
class GFG
{
    /* maxSumIS() returns the 
    maximum sum of increasing
    subsequence in arr[] of size n */
    static int maxSumIS(int arr[], int n)
    {
        int i, j, max = 0;
        int msis[] = new int[n];
  
        /* Initialize msis values 
           for all indexes */
        for (i = 0; i < n; i++)
            msis[i] = arr[i];
  
        /* Compute maximum sum values
           in bottom up manner */
        for (i = 1; i < n; i++)
            for (j = 0; j < i; j++)
                if (arr[i] > arr[j] &&
                    msis[i] < msis[j] + arr[i])
                    msis[i] = msis[j] + arr[i];
  
        /* Pick maximum of all
           msis values */
        for (i = 0; i < n; i++)
            if (max < msis[i])
                max = msis[i];
  
        return max;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int arr[] = new int[]{1, 101, 2, 3, 100, 4, 5};
        int n = arr.length;
        System.out.println("Sum of maximum sum "+
                            "increasing subsequence is "+
                              maxSumIS(arr, n));
    }
}
  
// This code is contributed 
// by Rajat Mishra 


Python3




# Dynamic Programming based Python 
# implementation of Maximum Sum 
# Increasing Subsequence (MSIS)
# problem
  
# maxSumIS() returns the maximum 
# sum of increasing subsequence 
# in arr[] of size n
def maxSumIS(arr, n):
    max = 0
    msis = [0 for x in range(n)]
  
    # Initialize msis values
    # for all indexes
    for i in range(n):
        msis[i] = arr[i]
  
    # Compute maximum sum 
    # values in bottom up manner
    for i in range(1, n):
        for j in range(i):
            if (arr[i] > arr[j] and
                msis[i] < msis[j] + arr[i]):
                msis[i] = msis[j] + arr[i]
  
    # Pick maximum of
    # all msis values
    for i in range(n):
        if max < msis[i]:
            max = msis[i]
  
    return max
  
# Driver Code
arr = [1, 101, 2, 3, 100, 4, 5]
n = len(arr)
print("Sum of maximum sum increasing " + 
                     "subsequence is " +
                  str(maxSumIS(arr, n)))
  
# This code is contributed 
# by Bhavya Jain


C#




// Dynamic Programming C# implementation
// of Maximum Sum Increasing Subsequence
// (MSIS) problem 
using System;
class GFG {
      
    // maxSumIS() returns the 
    // maximum sum of increasing
    // subsequence in arr[] of size n 
    static int maxSumIS( int []arr, int n )
    {
        int i, j, max = 0;
        int []msis = new int[n];
  
        /* Initialize msis values
           for all indexes */
        for ( i = 0; i < n; i++ )
            msis[i] = arr[i];
  
        /* Compute maximum sum values
           in bottom up manner */
        for ( i = 1; i < n; i++ )
            for ( j = 0; j < i; j++ )
                if ( arr[i] > arr[j] &&
                    msis[i] < msis[j] + arr[i])
                    msis[i] = msis[j] + arr[i];
  
        /* Pick maximum of all 
           msis values */
        for ( i = 0; i < n; i++ )
            if ( max < msis[i] )
                max = msis[i];
  
        return max;
    }
      
    // Driver Code
    public static void Main()
    {
        int []arr = new int[]{1, 101, 2, 3, 100, 4, 5};
        int n = arr.Length;
        Console.WriteLine("Sum of maximum sum increasing "+
                                        " subsequence is "+
        maxSumIS(arr, n));
    }
}
  
// This code is contributed by Sam007


PHP




<?php
// Dynamic Programming implementation
// of Maximum Sum Increasing
// Subsequence (MSIS) problem 
  
// maxSumIS() returns the maximum 
// sum of increasing subsequence
// in arr[] of size n 
function maxSumIS($arr, $n)
{
    $max = 0;
    $msis= array($n);
  
    // Initialize msis values
    // for all indexes 
    for($i = 0; $i < $n; $i++ )
        $msis[$i] = $arr[$i];
  
    // Compute maximum sum values
    // in bottom up manner 
    for($i = 1; $i < $n; $i++)
        for($j = 0; $j < $i; $j++)
            if ($arr[$i] > $arr[$j] && 
                $msis[$i] < $msis[$j] + $arr[$i])
                $msis[$i] = $msis[$j] + $arr[$i];
  
    // Pick maximum of all msis values
    for($i = 0;$i < $n; $i++ )
        if ($max < $msis[$i] )
            $max = $msis[$i];
  
    return $max;
}
  
    // Driver Code
    $arr = array(1, 101, 2, 3, 100, 4, 5);
    $n = count($arr);
    echo "Sum of maximum sum increasing subsequence is " 
                                   .maxSumIS( $arr, $n );
          
// This code is contributed by Sam007
?>


Javascript




<script>
  
// Dynamic Programming implementation 
// of Maximum Sum Increasing Subsequence 
// (MSIS) problem 
  
// maxSumIS() returns the maximum 
// sum of increasing subsequence 
// in arr[] of size n 
function maxSumIS(arr, n) 
{ 
    let i, j, max = 0; 
    let msis = new Array(n); 
  
    // Initialize msis values 
    // for all indexes 
    for(i = 0; i < n; i++) 
        msis[i] = arr[i]; 
  
    // Compute maximum sum values 
    // in bottom up manner 
    for(i = 1; i < n; i++) 
        for(j = 0; j < i; j++) 
            if (arr[i] > arr[j] && 
                msis[i] < msis[j] + arr[i]) 
                msis[i] = msis[j] + arr[i]; 
  
    // Pick maximum of 
    // all msis values 
    for(i = 0; i < n; i++) 
        if (max < msis[i]) 
            max = msis[i]; 
  
    return max; 
} 
  
// Driver Code 
let arr = [ 1, 101, 2, 3, 100, 4, 5 ]; 
let n = arr.length; 
document.write("Sum of maximum sum increasing " + 
               "subsequence is " + maxSumIS(arr, n)); 
                 
// This code is contributed by rishavmahato348
  
</script>


Output

Sum of maximum sum increasing subsequence is 106

Time Complexity: O(n^2) 
Space Complexity O(n) 



Similar Reads

Longest Increasing Subsequence using Longest Common Subsequence Algorithm
Given an array arr[] of N integers, the task is to find and print the Longest Increasing Subsequence.Examples: Input: arr[] = {12, 34, 1, 5, 40, 80} Output: 4 {12, 34, 40, 80} and {1, 5, 40, 80} are the longest increasing subsequences.Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60, 80} Output: 6 Prerequisite: LCS, LISApproach: The longest increasing
12 min read
Maximum Sum Increasing Subsequence using Binary Indexed Tree
Given an array of size n. Find the maximum sum of an increasing subsequence.Examples: Input : arr[] = { 1, 20, 4, 2, 5 } Output : Maximum sum of increasing subsequence is = 21 The subsequence 1, 20 gives maximum sum which is 21 Input : arr[] = { 4, 2, 3, 1, 5, 8 } Output : Maximum sum of increasing subsequence is = 18 The subsequence 2, 3, 5, 8 giv
10 min read
Maximum sum increasing subsequence from a prefix and a given element after prefix is must
Given an array of n positive integers, write a program to find the maximum sum of increasing subsequence from prefix till ith index and also including a given kth element which is after i, i.e., k &gt; i. Examples : Input: arr[] = {1, 101, 2, 3, 100, 4, 5} i-th index = 4 (Element at 4th index is 100) K-th index = 6 (Element at 6th index is 5.) Outp
14 min read
Maximum sum subsequence of any size which is decreasing-increasing alternatively
Given an array of integers arr[], find the subsequence with maximum sum whose elements are first decreasing, then increasing, or vice versa, The subsequence can start anywhere in the main sequence, not necessarily at the first element of the main sequence. A sequence {x1, x2, .. xn} is an alternating sequence if its elements satisfy one of the foll
15+ min read
Find heaviest increasing Subsequence with maximum sum in a String
Given a string s and an array arr[] representing the weights of each character in the string, the task is to find the heaviest increasing subsequence with the maximum sum and return that subsequence. Examples: Input: s = "acbde", arr[] = {2, 4, 3, 5, 1}Output: acdExplanation: The heaviest increasing subsequence with the maximum sum is "acd" as the
8 min read
POTD Solutions | 23 Oct’ 23 | Maximum Sum Increasing Subsequence
View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Dynamic Programming but will also help you build up problem-solving skills. We recommend you to try this
4 min read
Printing Maximum Sum Increasing Subsequence
The Maximum Sum Increasing Subsequence problem is to find the maximum sum subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. Examples: Input: [1, 101, 2, 3, 100, 4, 5] Output: [1, 2, 3, 100] Input: [3, 4, 5, 10] Output: [3, 4, 5, 10] Input: [10, 5, 4, 3] Output: [10] Input: [3, 2, 6, 4, 5, 1] O
15+ min read
Maximum product of an increasing subsequence
Given an array of numbers, find the maximum product formed by multiplying numbers of an increasing subsequence of that array. Note: A single number is supposed to be an increasing subsequence of size 1. Examples: Input : arr[] = { 3, 100, 4, 5, 150, 6 } Output : 45000 Maximum product is 45000 formed by the increasing subsequence 3, 100, 150. Note t
6 min read
Maximum score of Array using increasing subsequence and subarray with given conditions
Given an array arr[]. The task is to find the maximum score that can be achieved from arr[] for i=[1, N-2]. The conditions for scoring are given below. If arr[0...j] &lt; arr[i] &lt; arr[i+1...N-1], then score = 2.If arr[i-1] &lt; arr[i] &lt; arr[i+1] and previous condition is not satisfied, then score = 1.If none of the conditions holds, then scor
6 min read
Find the Increasing subsequence of length three with maximum product
Given a sequence of non-negative integers, find the subsequence of length 3 having maximum product with the numbers of the subsequence being in ascending order. Examples: Input: arr[] = {6, 7, 8, 1, 2, 3, 9, 10} Output: 8 9 10 Input: arr[] = {1, 5, 10, 8, 9} Output: 5 8 9 Approach: Since we want to find the maximum product, we need to find followin
10 min read
Maximum product of an increasing subsequence of size 3
Given an array of distinct positive integers, the task is to find the maximum product of increasing subsequence of size 3, i.e., we need to find arr[i]*arr[j]*arr[k] such that arr[i] &lt; arr[j] &lt; arr[k] and i &lt; j &lt; k &lt; n Examples: Input: arr[] = {10, 11, 9, 5, 6, 1, 20}Output: 2200 Explanation: Increasing sub-sequences of size three ar
14 min read
Longest Increasing Subsequence having sum value atmost K
Given an integer array arr[] of size N and an integer K. The task is to find the length of the longest subsequence whose sum is less than or equal to K. Example: Input: arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15} K = 40 Output: 5 Explanation: If we select subsequence {0, 1, 3, 7, 15} then total sum will be 26, which is less than
5 min read
Maximize sum of all elements which are not a part of the Longest Increasing Subsequence
Given an array arr[], the task is to find the maximum sum of all the elements which are not a part of the longest increasing subsequence. Examples: Input: arr[] = {4, 6, 1, 2, 3, 8} Output: 10 Explanation: Elements are 4 and 6 Input: arr[] = {5, 4, 3, 2, 1} Output: 14 Explanation: Elements are 5, 4, 3, 2 Recommended PracticeMaximum sum of elements
8 min read
Maximum length subsequence such that adjacent elements in the subsequence have a common factor
Given an array arr[], the task is to find the maximum length of a subsequence such that the adjacent elements in the subsequence have a common factor. Examples: Input: arr[] = { 13, 2, 8, 6, 3, 1, 9 } Output: 5Max length subsequence with satisfied conditions: { 2, 8, 6, 3, 9 } Input: arr[] = { 12, 2, 8, 6, 3, 1, 9 } Output: 6Max length subsequence
9 min read
Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
Given a string S, the task is to count the maximum occurrence of subsequences in the given string such that the indices of the characters of the subsequence are Arithmetic Progression. Examples: Input: S = "xxxyy" Output: 6 Explanation: There is a subsequence "xy", where indices of each character of the subsequence are in A.P. The indices of the di
12 min read
Longest Subsequence with absolute difference of pairs as at least Subsequence's maximum
Given an array arr[] of length N. The task is to find the length of the longest subsequence of the array such that the absolute difference between any pair of elements is greater than or equal to the maximum element in that subsequence. Examples: Input: N = 6, arr[] = {1, 1, 0, 0, 0, 0}Output: 4Explanation: Considering 0 as max element of subsequen
7 min read
Sum of maximum and minimum of Kth subset ordered by increasing subset sum
Given an integer N and a set of all non-negative powers of N as S = {N0, N1, N2, N3, ... }, arrange all non-empty subsets of S in increasing order of subset-sum. The task is to find the sum of the greatest and smallest elements of the Kth subset from that ordering. Examples: Input: N = 4, K = 3Output: 5Explanation: S = {1, 4, 16, 64, ...}.Therefore
7 min read
Maximum length Subsequence with alternating sign and maximum Sum
Given an array arr[] of size n having both positive and negative integer excluding zero. The task is to find the subsequence with an alternating sign having maximum size and maximum sum that is, in a subsequence sign of each adjacent element is opposite for example if the first one is positive then the second one has to be negative followed by anot
7 min read
Divide given numeric string into at most two increasing subsequences which form an increasing string upon concatenation
Given a string S consisting of N digits, the task is to partition the string into at most two increasing subsequences such that concatenation of them also forms an increasing string. If it is not possible to do so, then print "-1". Examples: Input: S = “040425524644”Output: 0022444 44556Explanation:One of the possible way to partition the given str
10 min read
Construction of Longest Increasing Subsequence (N log N)
In my previous post, I have explained about longest increasing sub-sequence (LIS) problem in detail. However, the post only covered code related to querying size of LIS, but not the construction of LIS. I left it as an exercise. If you have solved, cheers. If not, you are not alone, here is code. If you have not read my previous post, read here. No
10 min read
Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
Given an array of random numbers, find the longest monotonically increasing subsequence (LIS) in the array. If you want to understand the O(NlogN) approach, it's explained very clearly here. In this post, a simple and time-saving implementation of O(NlogN) approach using stl is discussed. Below is the code for LIS O(NlogN): Implementation: C/C++ Co
5 min read
Longest Increasing Odd Even Subsequence
Given an array of size n. The problem is to find the length of the subsequence in the given array such that all the elements of the subsequence are sorted in increasing order and also they are alternately odd and even. Note that the subsequence could start either with the odd number or with the even number. Examples: Input : arr[] = {5, 6, 9, 4, 7,
8 min read
Printing longest Increasing consecutive subsequence
Given n elements, write a program that prints the longest increasing subsequence whose adjacent element difference is one. Examples: Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output : 3 4 5 6 7 8 Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs by one. Input : a[] = {6, 7, 8, 3, 4, 5, 9, 10} O
8 min read
Number of Permutations such that no Three Terms forms Increasing Subsequence
Given a number N. The task is to find the number of permutations of 1 to N such that no three terms of the permutation form an increasing subsequence. Examples: Input : N = 3 Output : 5 Valid permutations : 132, 213, 231, 312 and 321 and not 123 Input : N = 4 Output : 14 The above problem is an application of Catalan numbers. So, the task is to onl
5 min read
Length of the longest increasing subsequence such that no two adjacent elements are coprime
Given an array arr[] of size N. The task is to find the length of the longest subsequence from the given array such that the sequence is strictly increasing and no two adjacent elements are coprime. Note: The elements in the given array are strictly increasing in order (1 &lt;= a[i] &lt;= 105) Examples: Input : a[] = { 1, 2, 3, 4, 5, 6} Output : 3
9 min read
Divide array into increasing and decreasing subsequence without changing the order
Given a merged sequence which consists of two sequences which got merged, one of them was strictly increasing and the other was strictly decreasing. Elements of increasing sequence were inserted between elements of the decreasing one without changing the order. Sequences [1, 3, 4] and [10, 4, 2] can produce the following resulting sequences: [10, 1
11 min read
Length of longest increasing subsequence in a string
Given a string S, the task is to find the length of the longest increasing subsequence present in the given string. A sequence of characters placed in increasing order of their ASCII values is called an increasing sequence. Examples: Input: S = "abcfgffs"Output: 6Explanation: Subsequence "abcfgs" is the longest increasing subsequence present in the
7 min read
Longest Increasing Subsequence using BIT
Given an array arr, the task is to find the length of The Longest Increasing Sequence using Binary Indexed Tree (BIT) Examples: Input: arr = {6, 5, 1, 3, 2, 4, 8, 7} Output: 4 Explanation: The possible subsequences are {1 2 4 7}, {1 2 4 8}, {1 3 4 8}, {1 3 4 7} Now insert the elements one by one from left to right: 6 is inserted: max length till 5
13 min read
Length of longest increasing index dividing subsequence
Given an array arr[] of size N, the task is to find the longest increasing sub-sequence such that index of any element is divisible by index of previous element (LIIDS). The following are the necessary conditions for the LIIDS:If i, j are two indices in the given array. Then: i &lt; jj % i = 0arr[i] &lt; arr[j] Examples: Input: arr[] = {1, 2, 3, 7,
7 min read
Longest increasing subsequence which forms a subarray in the sorted representation of the array
Given an array arr[] of N integers, the task is to find the length of the longest increasing subsequence such that it forms a subarray when the original array is sorted. Examples: Input: arr[] = { 2, 6, 4, 8, 2, 9 }Output: 3Explanation: Sorted array: {2, 2, 4, 6, 8, 9}All possible non-decreasing sequences of the original array are {2}, {6}, {4}, {8
11 min read