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++
#include <bits/stdc++.h>
using namespace std;
 Â
int maxSumIS( int arr[], int n)Â
{Â
    int i, j, max = 0;Â
    int msis[n];Â
 Â
   Â
   Â
    for ( i = 0; i < n; i++ )Â
        msis[i] = arr[i];Â
 Â
   Â
   Â
    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];Â
 Â
   Â
   Â
    for ( i = 0; i < n; i++ )Â
        if ( max < msis[i] )Â
            max = msis[i];Â
 Â
    return max;Â
}Â
 Â
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;Â
}Â
 Â
|
C
#include<stdio.h>
 Â
  Â
  Â
int maxSumIS( int arr[], int n)
{
    int i, j, max = 0;
    int msis[n];
 Â
   Â
      Â
    for ( i = 0; i < n; i++ )
        msis[i] = arr[i];
 Â
   Â
      Â
    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];
 Â
   Â
      Â
    for ( i = 0; i < n; i++ )
        if ( max < msis[i] )
            max = msis[i];
 Â
    return max;
}
 Â
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
  Â
  Â
  Â
class GFG
{
   Â
   Â
   Â
    static int maxSumIS( int arr[], int n)
    {
        int i, j, max = 0 ;
        int msis[] = new int [n];
 Â
       Â
          Â
        for (i = 0 ; i < n; i++)
            msis[i] = arr[i];
 Â
       Â
          Â
        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];
 Â
       Â
          Â
        for (i = 0 ; i < n; i++)
            if (max < msis[i])
                max = msis[i];
 Â
        return max;
    }
 Â
   Â
    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));
    }
}
 Â
|
Python3
 Â
def maxSumIS(arr, n):
    max = 0
    msis = [ 0 for x in range (n)]
 Â
   Â
   Â
    for i in range (n):
        msis[i] = arr[i]
 Â
   Â
   Â
    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]
 Â
   Â
   Â
    for i in range (n):
        if max < msis[i]:
            max = msis[i]
 Â
    return max
 Â
arr = [ 1 , 101 , 2 , 3 , 100 , 4 , 5 ]
n = len (arr)
print ( "Sum of maximum sum increasing " + Â
                     "subsequence is " +
                  str (maxSumIS(arr, n)))
 Â
|
C#
using System;
class GFG {
     Â
   Â
   Â
   Â
    static int maxSumIS( int []arr, int n )
    {
        int i, j, max = 0;
        int []msis = new int [n];
 Â
       Â
          Â
        for ( i = 0; i < n; i++ )
            msis[i] = arr[i];
 Â
       Â
          Â
        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];
 Â
       Â
          Â
        for ( i = 0; i < n; i++ )
            if ( max < msis[i] )
                max = msis[i];
 Â
        return max;
    }
     Â
   Â
    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));
    }
}
 Â
|
PHP
<?php
 Â
function maxSumIS( $arr , $n )
{
    $max = 0;
    $msis = array ( $n );
 Â
   Â
   Â
    for ( $i = 0; $i < $n ; $i ++ )
        $msis [ $i ] = $arr [ $i ];
 Â
   Â
   Â
    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 ];
 Â
   Â
    for ( $i = 0; $i < $n ; $i ++ )
        if ( $max < $msis [ $i ] )
            $max = $msis [ $i ];
 Â
    return $max ;
}
 Â
   Â
    $arr = array (1, 101, 2, 3, 100, 4, 5);
    $n = count ( $arr );
    echo "Sum of maximum sum increasing subsequence is " Â
                                   .maxSumIS( $arr , $n );
         Â
?>
|
Javascript
<script>
 Â
 Â
function maxSumIS(arr, n)Â
{Â
    let i, j, max = 0;Â
    let msis = new Array(n);Â
 Â
   Â
   Â
    for (i = 0; i < n; i++)Â
        msis[i] = arr[i];Â
 Â
   Â
   Â
    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];Â
 Â
   Â
   Â
    for (i = 0; i < n; i++)Â
        if (max < msis[i])Â
            max = msis[i];Â
 Â
    return max;Â
}Â
 Â
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));Â
                Â
 Â
</script>
|
Output
Sum of maximum sum increasing subsequence is 106
Time Complexity: O(n^2)Â
Space Complexity O(n)Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!