Maximize the Sum of a Subsequence from an Array based on given conditions
Last Updated :
31 May, 2021
Given an array a[] consisting of N integers, the task is to perform the following operations:
- Select a subsequence and for every pth element of the subsequence, calculate the product p * a[i].
- Calculate the sum of the calculated values of p * a[i].
- The subsequence should be selected such that it maximizes the desired sum.
Examples:
Input: N = 3, a[] = {-1, 3, 4}
Output: 17
Explanation:
The subsequence {-1, 3, 4} maximizes the sum = 1(-1) + 2(3) + 3(4) = 17
Input: N = 5, a[] = {-1, -9, 0, 5, -7}
Output: 14
Explanation:
The subsequence {-1, 0, 5} maximizes the sum = 1(-1) + 2(0) + 3(5) = 14
Naive Approach: The simplest approach to solve the problem is to generate all possible subsequences from the array and calculate the sum for each subsequence. Finally, find the maximum sum.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by using Dynamic Programming. Follow the steps below to solve the problem:
- For every element, two possibilities exist, that is, either the element is part of the subsequence or is not.
- Initialize a dp[][] matrix, where dp[i][j] stores the maximum of:
- Sum generated by selecting a[i] as the jth element of the subsequence, that is:
a[i] * j + maximumSum(j + 1, i + 1)
- Keep updating the dp[][] table by considering the above conditions for each array element and print the maximum sum possible from the entire array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 6;
int maximumSum( int a[], int count,
int index, int n,
int dp[N][N])
{
if (index == n)
return 0;
if (dp[index][count] != -1)
return dp[index][count];
int take_element = a[index] * count +
maximumSum(a, count + 1,
index + 1, n, dp);
int dont_take = maximumSum(a, count,
index + 1, n, dp);
return dp[index][count] = max(take_element,
dont_take);
}
int main()
{
int n = 5;
int a[] = { -1, -9, 0, 5, -7 };
int dp[N][N];
memset (dp, -1, sizeof (dp));
cout << (maximumSum(a, 1, 0, n, dp));
}
|
Java
import java.util.*;
public class GFG {
public static int maximumSum( int [] a, int count,
int index, int n,
int [][] dp)
{
if (index == n)
return 0 ;
if (dp[index][count] != - 1 )
return dp[index][count];
int take_element
= a[index] * count
+ maximumSum(a, count + 1 ,
index + 1 , n, dp);
int dont_take
= maximumSum(a, count, index + 1 , n, dp);
return dp[index][count]
= Math.max(take_element, dont_take);
}
public static void main(String args[])
{
int n = 5 ;
int a[] = { - 1 , - 9 , 0 , 5 , - 7 };
int dp[][] = new int [n + 1 ][n + 1 ];
for ( int i[] : dp)
Arrays.fill(i, - 1 );
System.out.println(maximumSum(a, 1 , 0 , n, dp));
}
}
|
Python3
def maximumSum(a, count, index, n, dp):
if (index = = n):
return 0
if (dp[index][count] ! = - 1 ):
return dp[index][count]
take_element = (a[index] * count +
maximumSum(a, count + 1 ,
index + 1 ,
n, dp))
dont_take = maximumSum(a, count,
index + 1 , n, dp)
dp[index][count] = max (take_element,
dont_take)
return dp[index][count]
n = 5
a = [ - 1 , - 9 , 0 , 5 , - 7 ]
dp = [[ - 1 for x in range (n + 1 )]
for y in range (n + 1 )]
print (maximumSum(a, 1 , 0 , n, dp))
|
C#
using System;
class GFG{
public static int maximumSum( int [] a, int count,
int index, int n,
int [,] dp)
{
if (index == n)
return 0;
if (dp[index, count] != -1)
return dp[index, count];
int take_element = a[index] * count +
maximumSum(a, count + 1,
index + 1,
n, dp);
int dont_take = maximumSum(a, count,
index + 1, n, dp);
return dp[index, count] = Math.Max(take_element,
dont_take);
}
public static void Main(String []args)
{
int n = 5;
int []a = { -1, -9, 0, 5, -7 };
int [,]dp = new int [n + 1, n + 1];
for ( int i = 0; i < n + 1; i++)
{
for ( int j = 0; j < n + 1; j++)
{
dp[i, j] = -1;
}
}
Console.WriteLine(maximumSum(a, 1, 0, n, dp));
}
}
|
Javascript
<script>
var N = 6;
function maximumSum(a, count, index, n, dp)
{
if (index == n)
return 0;
if (dp[index][count] != -1)
return dp[index][count];
var take_element = a[index] * count +
maximumSum(a, count + 1,
index + 1, n, dp);
var dont_take = maximumSum(a, count,
index + 1, n, dp);
return dp[index][count] = Math.max(take_element,
dont_take);
}
var n = 5;
var a = [ -1, -9, 0, 5, -7];
var dp = Array.from(Array(N), ()=>Array(N).fill(-1));
document.write(maximumSum(a, 1, 0, n, dp));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Share your thoughts in the comments
Please Login to comment...