Maximum Sum Decreasing Subsequence
Last Updated :
30 Jan, 2023
Given an array of N positive integers. The task is to find the sum of the maximum sum decreasing subsequence(MSDS) of the given array such that the integers in the subsequence are sorted in decreasing order.
Examples:
Input: arr[] = {5, 4, 100, 3, 2, 101, 1}
Output: 106
100 + 3 + 2 + 1 = 106
Input: arr[] = {10, 5, 4, 3}
Output: 22
10 + 5 + 4 + 3 = 22
This problem is a variation of the Longest Decreasing Subsequence problem. The Optimal Substructure for the above problem will be:
Let arr[0..n-1] be the input array and MSDS[i] be the maximum sum of the MSDS ending at index i such that arr[i] is the last element of the MSDS.
Then, MSDS[i] can be written as:
MSDS[i] = a[i] + max( MSDS[j] ) where i > j > 0 and arr[j] > arr[i] or,
MSDS[i] = a[i], if no such j exists.
To find the MSDS for a given array, we need to return max(MSDS[i]) where n > i > 0.
Steps to solve the problem:
1. declare variables i,j and max=0.
2. declare an msds array of size n.
3. iterate through i=0 until n:
* assign arr[i] to msds[i].
4. iterate through i=1 until n:
* iterate through j=0 until n:
* check if arr[i] < arr[j]&&msds[i] < msds[j]+arr[i] than update msds[i] to sum of msds[j] and arr[i].
5. iterate through =0 until n:
* check if max is smaller than msds[i] than update max to msds[i].
6. return max.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSumDS( int arr[], int n)
{
int i, j, max = 0;
int MSDS[n];
for (i = 0; i < n; i++)
MSDS[i] = arr[i];
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] < arr[j] && MSDS[i] < MSDS[j] + arr[i])
MSDS[i] = MSDS[j] + arr[i];
for (i = 0; i < n; i++)
if (max < MSDS[i])
max = MSDS[i];
return max;
}
int main()
{
int arr[] = { 5, 4, 100, 3, 2, 101, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Sum of maximum sum decreasing subsequence is: "
<< maxSumDS(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
class GfG {
public static int maxSumDS( int arr[], int n)
{
int i, j, max = 0 ;
int [] MSDS = new int [n];
for (i = 0 ; i < n; i++)
MSDS[i] = arr[i];
for (i = 1 ; i < n; i++)
for (j = 0 ; j < i; j++)
if (arr[i] < arr[j] &&
MSDS[i] < MSDS[j] + arr[i])
MSDS[i] = MSDS[j] + arr[i];
for (i = 0 ; i < n; i++)
if (max < MSDS[i])
max = MSDS[i];
return max;
}
public static void main(String argc[])
{
int arr[] = { 5 , 4 , 100 , 3 , 2 , 101 , 1 };
int n = 7 ;
System.out.println( "Sum of maximum sum"
+ " decreasing subsequence is: "
+ maxSumDS(arr, n));
}
}
|
Python3
def maxSumDS(arr, n):
i, j, max = ( 0 , 0 , 0 )
MSDS = [ 0 for i in range (n)]
for i in range (n):
MSDS[i] = arr[i]
for i in range ( 1 , n):
for j in range (i):
if (arr[i] < arr[j] and
MSDS[i] < MSDS[j] + arr[i]):
MSDS[i] = MSDS[j] + arr[i]
for i in range (n):
if ( max < MSDS[i]):
max = MSDS[i]
return max
if __name__ = = "__main__" :
arr = [ 5 , 4 , 100 , 3 ,
2 , 101 , 1 ]
n = len (arr)
print ( "Sum of maximum sum decreasing subsequence is: " ,
maxSumDS(arr, n))
|
C#
using System;
class GFG
{
public static int maxSumDS( int []arr,
int n)
{
int i, j, max = 0;
int [] MSDS = new int [n];
for (i = 0; i < n; i++)
MSDS[i] = arr[i];
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] < arr[j] &&
MSDS[i] < MSDS[j] + arr[i])
MSDS[i] = MSDS[j] + arr[i];
for (i = 0; i < n; i++)
if (max < MSDS[i])
max = MSDS[i];
return max;
}
static public void Main ()
{
int []arr = {5, 4, 100,
3, 2, 101, 1};
int n = 7;
Console.WriteLine( "Sum of maximum sum" +
" decreasing subsequence is: " +
maxSumDS(arr, n));
}
}
|
PHP
<?php
function maxSumDS( $arr , $n )
{
$i ; $j ; $max = 0;
$MSDS = array ();
for ( $i = 0; $i < $n ; $i ++)
$MSDS [ $i ] = $arr [ $i ];
for ( $i = 1; $i < $n ; $i ++)
for ( $j = 0; $j < $i ; $j ++)
if ( $arr [ $i ] < $arr [ $j ] &&
$MSDS [ $i ] < $MSDS [ $j ] + $arr [ $i ])
$MSDS [ $i ] = $MSDS [ $j ] + $arr [ $i ];
for ( $i = 0; $i < $n ; $i ++)
if ( $max < $MSDS [ $i ])
$max = $MSDS [ $i ];
return $max ;
}
$arr = array (5, 4, 100,
3, 2, 101, 1 );
$n = sizeof( $arr );
echo "Sum of maximum sum decreasing " .
"subsequence is: " ,
maxSumDS( $arr , $n );
?>
|
Javascript
<script>
function maxSumDS(arr, n)
{
let i, j, max = 0;
let MSDS = new Array(n);
for (i = 0; i < n; i++)
MSDS[i] = arr[i];
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] < arr[j] &&
MSDS[i] < MSDS[j] + arr[i])
MSDS[i] = MSDS[j] + arr[i];
for (i = 0; i < n; i++)
if (max < MSDS[i])
max = MSDS[i];
return max;
}
let arr = [5, 4, 100, 3, 2, 101, 1];
let n = 7;
document.write( "Sum of maximum sum" +
" decreasing subsequence is: " +
maxSumDS(arr, n));
</script>
|
Output
Sum of maximum sum decreasing subsequence is: 106
Complexity Analysis:
- Time complexity: O(N2)
- Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...