Maximum sum increasing subsequence from a prefix and a given element after prefix is must
Last Updated :
09 Dec, 2022
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 > 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.)
Output: 11
Explanation:
So we need to calculate the maximum sum of subsequence (1 101 2 3 100 5) such that 5 is necessarily included in the subsequence, so answer is 11 by subsequence (1 2 3 5).
Input: arr[] = {1, 101, 2, 3, 100, 4, 5} i-th index = 2 (Element at 2nd index is 2) K-th index = 5 (Element at 5th index is 4.)
Output: 7
Explanation:
So we need to calculate the maximum sum of subsequence (1 101 2 4) such that 4 is necessarily included in the subsequence, so answer is 7 by subsequence (1 2 4).
Prerequisite : Maximum Sum Increasing Subsequence
Naive Approach:
- Construct a new array containing elements till ith index and the kth element.
- Recursively calculate all the increasing subsequences.
- Discard all the subsequences not having kth element included.
- Calculate the maximum sum from the left over subsequences and display it.
Time Complexity: O(2n)
Better Approach: Use a dynamic approach to maintain a table dp[][]. The value of dp[i][k] stores the maximum sum of increasing subsequence till ith index and containing the kth element.
C++
// C++ program to find maximum sum increasing
// subsequence till i-th index and including
// k-th index.
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll pre_compute(ll a[], ll n, ll index, ll k)
{
ll dp[n][n] = { 0 };
// Initializing the first row of the dp[][].
for (int i = 0; i < n; i++) {
if (a[i] > a[0])
dp[0][i] = a[i] + a[0];
else
dp[0][i] = a[i];
}
// Creating the dp[][] matrix.
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[j] > a[i] && j > i) {
if (dp[i - 1][i] + a[j] > dp[i - 1][j])
dp[i][j] = dp[i - 1][i] + a[j];
else
dp[i][j] = dp[i - 1][j];
}
else
dp[i][j] = dp[i - 1][j];
}
}
// To calculate for i=4 and k=6.
return dp[index][k];
}
int main()
{
ll a[] = { 1, 101, 2, 3, 100, 4, 5 };
ll n = sizeof(a) / sizeof(a[0]);
ll index = 4, k = 6;
printf("%lld", pre_compute(a, n, index, k));
return 0;
}
Java
// Java program to find maximum sum increasing
// subsequence till i-th index and including
// k-th index.
import java.io.*;
class GFG {
static int pre_compute(int a[], int n,
int index, int k)
{
int dp[][] = new int[n][n];
// Initializing the first row of
// the dp[][].
for (int i = 0; i < n; i++) {
if (a[i] > a[0])
dp[0][i] = a[i] + a[0];
else
dp[0][i] = a[i];
}
// Creating the dp[][] matrix.
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[j] > a[i] && j > i)
{
if (dp[i - 1][i] + a[j] >
dp[i - 1][j])
dp[i][j] = dp[i - 1][i]
+ a[j];
else
dp[i][j] = dp[i - 1][j];
}
else
dp[i][j] = dp[i - 1][j];
}
}
// To calculate for i=4 and k=6.
return dp[index][k];
}
// Driver code
public static void main(String[] args)
{
int a[] = { 1, 101, 2, 3, 100, 4, 5 };
int n = a.length;
int index = 4, k = 6;
System.out.println(
pre_compute(a, n, index, k));
}
}
// This code is contributed by Smitha.
Python3
# Python3 program to find maximum
# sum increasing subsequence till
# i-th index and including k-th index.
def pre_compute(a, n, index, k):
dp = [[0 for i in range(n)]
for i in range(n)]
# Initializing the first
# row of the dp[][]
for i in range(n):
if a[i] > a[0]:
dp[0][i] = a[i] + a[0]
else:
dp[0][i] = a[i]
# Creating the dp[][] matrix.
for i in range(1, n):
for j in range(n):
if a[j] > a[i] and j > i:
if dp[i - 1][i] + a[j] > dp[i - 1][j]:
dp[i][j] = dp[i - 1][i] + a[j]
else:
dp[i][j] = dp[i - 1][j]
else:
dp[i][j] = dp[i - 1][j]
# To calculate for i=4 and k=6.
return dp[index][k]
# Driver code
a = [1, 101, 2, 3, 100, 4, 5 ]
n = len(a)
index = 4
k = 6
print(pre_compute(a, n, index, k))
# This code is contributed
# by sahilshelangia
C#
// C# program to find maximum
// sum increasing subsequence
// till i-th index and including
// k-th index.
using System;
class GFG
{
static int pre_compute(int []a, int n,
int index, int k)
{
int [,]dp = new int[n, n];
// Initializing the first
// row of the dp[][].
for (int i = 0; i < n; i++)
{
if (a[i] > a[0])
dp[0, i] = a[i] + a[0];
else
dp[0, i] = a[i];
}
// Creating the dp[][] matrix.
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[j] > a[i] && j > i)
{
if (dp[i - 1, i] + a[j] >
dp[i - 1, j])
dp[i, j] = dp[i - 1, i] +
a[j];
else
dp[i, j] = dp[i - 1, j];
}
else
dp[i, j] = dp[i - 1, j];
}
}
// To calculate for i=4 and k=6.
return dp[index, k];
}
// Driver code
static public void Main ()
{
int []a = {1, 101, 2,
3, 100, 4, 5};
int n = a.Length;
int index = 4, k = 6;
Console.WriteLine(pre_compute(a, n,
index, k));
}
}
// This code is contributed by @ajit
PHP
<?php
// PHP program to find maximum sum increasing
// subsequence till i-th index and including
// k-th index.
function pre_compute(&$a, $n, $index, $k)
{
$dp = array_fill(0, $n,
array_fill(0, $n, NULL));
// Initializing the first row of the dp[][].
for ($i = 0; $i < $n; $i++)
{
if ($a[$i] > $a[0])
$dp[0][$i] = $a[$i] + $a[0];
else
$dp[0][$i] = $a[$i];
}
// Creating the dp[][] matrix.
for ($i = 1; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
{
if ($a[$j] > $a[$i] && $j > $i)
{
if (($dp[$i - 1][$i] + $a[$j]) >
$dp[$i - 1][$j])
$dp[$i][$j] = $dp[$i - 1][$i] +
$a[$j];
else
$dp[$i][$j] = $dp[$i - 1][$j];
}
else
$dp[$i][$j] = $dp[$i - 1][$j];
}
}
// To calculate for i=4 and k=6.
return $dp[$index][$k];
}
// Driver Code
$a = array( 1, 101, 2, 3, 100, 4, 5 );
$n = sizeof($a);
$index = 4;
$k = 6;
echo pre_compute($a, $n, $index, $k);
// This code is contributed by ita_c
?>
JavaScript
<script>
// Javascript program to find maximum
// sum increasing subsequence till
// i-th index and including k-th index.
function pre_compute(a, n, index, k)
{
let dp = new Array(n);
for(let i = 0; i < n; i++)
{
dp[i] = new Array(n);
for(let j = 0; j < n; j++)
{
dp[i][j] = 0;
}
}
// Initializing the first row of
// the dp[][].
for(let i = 0; i < n; i++)
{
if (a[i] > a[0])
dp[0][i] = a[i] + a[0];
else
dp[0][i] = a[i];
}
// Creating the dp[][] matrix.
for(let i = 1; i < n; i++)
{
for(let j = 0; j < n; j++)
{
if (a[j] > a[i] && j > i)
{
if (dp[i - 1][i] + a[j] >
dp[i - 1][j])
dp[i][j] = dp[i - 1][i] +
a[j];
else
dp[i][j] = dp[i - 1][j];
}
else
dp[i][j] = dp[i - 1][j];
}
}
// To calculate for i=4 and k=6.
return dp[index][k];
}
// Driver code
let a = [ 1, 101, 2, 3, 100, 4, 5 ];
let n = a.length;
let index = 4, k = 6;
document.write(pre_compute(a, n, index, k));
// This code is contributed by mukesh07
</script>
Time Complexity: O(n2)
Auxiliary Space: O(n2)
Efficient approach: This problem is basically finding of maximum sum of increasing sub-sequence up to the given index i that all the elements of the sub-sequence is less than the kth (index) element or arr[k]. Hence, find the Maximum Sum Increasing Subsequence.
For example: arr[] = {1, 101, 2, 3, 100, 4, 5}, index = 4; k = 6;
Now, we need to just find the max sum sub-sequence from the array till index 4 given that all the elements of that sub-sequence is less that arr[k] which is 5. Now, iterating through the array.
For i = 0; as 1 < 5; max increasing sub-sequence {1}, max = 1.
For i = 1; as 101 > 5; skip this entry. Max increasing sub-sequence {1}, max = 1.
For i = 2; as 2 < 5; max increasing sub-sequence {1, 2}, max = 3.
For i = 3; as 3 < 5; max increasing sub-sequence {1, 2, 3}, max = 6.
For i = 4; as 100 > 5; skip this entry. Max increasing sub-sequence {1, 2, 3}, max = 6.
as index = 4; hence stop here and answer will be max + a[k] = 6 + 5 = 11.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
// Function to find the
// maximum of two numbers
int max(int a, int b)
{
if (a > b) {
return a;
}
return b;
}
// Function to find the sum
int pre_compute(int a[], int n, int index, int k)
{
// Base case
if (index >= k) {
return -1;
}
// Initialize the dp table
int dp[index] = { 0 };
int i;
// Initialize the dp array with
// corresponding array index value
for (i = 0; i <= index; i++) {
dp[i] = a[i];
}
int maxi = INT_MIN;
for (i = 0; i <= index; i++) {
// Only include values
// which are less than a[k]
if (a[i] >= a[k]) {
continue;
}
for (int j = 0; j < i; j++) {
// Check if a[i] is
// greater than a[j]
if (a[i] > a[j]) {
dp[i] = dp[j] + a[i];
}
// Update maxi
maxi = max(maxi, dp[i]);
}
}
// Incase all the elements in
// the array upto ith index
// are greater or equal to a[k]
if (maxi == INT_MIN) {
return a[k];
}
return maxi + a[k];
// Contributed by Mainak Dutta
}
// Driver code
int main()
{
int a[] = { 1, 101, 2, 3, 100, 4, 5 };
int n = sizeof(a) / sizeof(a[0]);
int index = 4, k = 6;
// Function call
printf("%d", pre_compute(a, n, index, k));
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the
// maximum of two numbers
static int max(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
// Function to find the sum
static int pre_compute(int a[], int n,
int index, int k)
{
// Base case
if (index >= k)
{
return -1;
}
// Initialize the dp table
int[] dp = new int[index + 1];
int i;
// Initialize the dp array with
// corresponding array index value
for(i = 0; i <= index; i++)
{
dp[i] = a[i];
}
int maxi = Integer.MIN_VALUE;
for(i = 0; i <= index; i++)
{
// Only include values
// which are less than a[k]
if (a[i] >= a[k])
{
continue;
}
for(int j = 0; j < i; j++)
{
// Check if a[i] is
// greater than a[j]
if (a[i] > a[j])
{
dp[i] = dp[j] + a[i];
}
// Update maxi
maxi = max(maxi, dp[i]);
}
}
// Incase all the elements in
// the array upto ith index
// are greater or equal to a[k]
if (maxi == Integer.MIN_VALUE)
{
return a[k];
}
return maxi + a[k];
}
// Driver code
public static void main (String[] args)
{
int a[] = { 1, 101, 2, 3, 100, 4, 5 };
int n = a.length;
int index = 4, k = 6;
System.out.println(pre_compute(a, n, index, k));
}
}
// This code is contributed by rag2127
Python3
# Python3 program for the above approach
# Function to find the sum
def pre_compute(a, n, index, k):
# Base case
if (index >= k):
return -1
# Initialize the dp table
dp = [0 for i in range(index)]
# Initialize the dp array with
# corresponding array index value
for i in range(index):
dp[i] = a[i]
maxi = -float('inf')
for i in range(index):
# Only include values
# which are less than a[k]
if (a[i] >= a[k]):
continue
for j in range(i):
# Check if a[i] is
# greater than a[j]
if (a[i] > a[j]):
dp[i] = dp[j] + a[i]
# Update maxi
maxi = max(maxi, dp[i])
# Incase all the elements in
# the array upto ith index
# are greater or equal to a[k]
if (maxi == -float('inf')):
return a[k]
return maxi + a[k]
# Driver code
a = [ 1, 101, 2, 3, 100, 4, 5 ]
n = len(a)
index = 4
k = 6
# Function call
print(pre_compute(a, n, index, k))
# This code is contributed by rohitsingh07052
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the
// maximum of two numbers
static int max(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
// Function to find the sum
static int pre_compute(int[] a, int n,
int index, int k)
{
// Base case
if (index >= k)
{
return -1;
}
// Initialize the dp table
int[] dp = new int[index + 1];
int i;
// Initialize the dp array with
// corresponding array index value
for(i = 0; i <= index; i++)
{
dp[i] = a[i];
}
int maxi = Int32.MinValue;
for(i = 0; i <= index; i++)
{
// Only include values
// which are less than a[k]
if (a[i] >= a[k])
{
continue;
}
for(int j = 0; j < i; j++)
{
// Check if a[i] is
// greater than a[j]
if (a[i] > a[j])
{
dp[i] = dp[j] + a[i];
}
// Update maxi
maxi = Math.Max(maxi, dp[i]);
}
}
// Incase all the elements in
// the array upto ith index
// are greater or equal to a[k]
if (maxi == Int32.MinValue)
{
return a[k];
}
return maxi + a[k];
}
// Driver code
static public void Main()
{
int[] a = { 1, 101, 2, 3, 100, 4, 5 };
int n = a.Length;
int index = 4, k = 6;
Console.WriteLine(pre_compute(a, n, index, k));
}
}
// This code is contributed by avanitrachhadiya2155
JavaScript
<script>
// Javascript program for the above approach
// Function to find the
// maximum of two numbers
function max(a, b)
{
if (a > b)
{
return a;
}
return b;
}
// Function to find the sum
function pre_compute(a, n, index, k)
{
// Base case
if (index >= k)
{
return -1;
}
// Initialize the dp table
let dp = new Array(index + 1);
dp.fill(0);
let i;
// Initialize the dp array with
// corresponding array index value
for(i = 0; i <= index; i++)
{
dp[i] = a[i];
}
let maxi = Number.MIN_VALUE;
for(i = 0; i <= index; i++)
{
// Only include values
// which are less than a[k]
if (a[i] >= a[k])
{
continue;
}
for(let j = 0; j < i; j++)
{
// Check if a[i] is
// greater than a[j]
if (a[i] > a[j])
{
dp[i] = dp[j] + a[i];
}
// Update maxi
maxi = Math.max(maxi, dp[i]);
}
}
// Incase all the elements in
// the array upto ith index
// are greater or equal to a[k]
if (maxi == Number.MIN_VALUE)
{
return a[k];
}
return maxi + a[k];
}
// Driver code
let a = [ 1, 101, 2, 3, 100, 4, 5 ];
let n = a.length;
let index = 4, k = 6;
document.write(pre_compute(a, n, index, k));
// This code is contributed by divyeshrabadiya07
</script>
Time Complexity: O( index2 )
Auxiliary Space: O( index )
Similar Reads
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
8 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 alternatin
15+ min read
Length of longest increasing prime subsequence from a given array
Given an array arr[] consisting of N positive integers, the task is to find the length of the longest increasing subsequence consisting of Prime Numbers in the given array. Examples: Input: arr[] = {1, 2, 5, 3, 2, 5, 1, 7}Output: 4Explanation:The Longest Increasing Prime Subsequence is {2, 3, 5, 7}.
9 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,
8 min read
Subsequence pair from given Array having all unique and all same elements respectively
Given an array arr[] of N integers, the task is to choose the two subsequences of equal lengths such that the first subsequence must have all the unique elements and the second subsequence must have all the same elements. Print the maximum length of the subsequence pair. Examples: Input: arr[] = {1,
6 min read
Print all subsequences in first decreasing then increasing by selecting N/2 elements from [1, N]
Given a positive integer N, the task is to print all the subsequences of the array in such a way that the subsequence is first decreasing then increasing by selecting ceil(N/2) elements from 1 to N. Examples: Input: N = 5Output :(2, 1, 3), (2, 1, 4), (2, 1, 5), (3, 1, 2), (3, 1, 4), (3, 1, 5), (3, 2
11 min read
Minimum number of elements which are not part of Increasing or decreasing subsequence in array
Given an array of n elements. Make strictly increasing and strictly decreasing subsequences from the array such that each array element belongs to increasing subsequence or decreasing subsequence, but not both, or can be part of none of the subsequence. Minimize the number of elements which are not
12 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] < arr[i] < arr[i+1...N-1], then score = 2.If arr[i-1] < arr[i] < arr[i+1] and previous condition is not satisfied, then sc
6 min read
Maximum sum subsequence made up of at most K distant elements including the first and last array elements
Given an array arr[] consisting of N integers and an integer K, the task is to print the maximum sum possible in a subsequence satisfying the following conditions: The elements arr[N - 1] and arr[0] are included in the subsequence.Adjacent elements in the subsequence can be at a distance of at most
8 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:
10 min read