Maximize the sum by choosing a Subsequence
Last Updated :
13 Mar, 2023
Given an array X[] of length N along with an integer K. Then the task is to choose a sub-sequence to maximize the sum by selecting the starting index i
(1 ? i ? N ) and make the subsequence of all the elements, which are at a continuous distance (i + K) till we are not out of the array X[] or at the last index.
Examples:
Input: N = 5, K = 2, X[] = {2, 5, 5, 8, 2}
Output: 13
Explanation: The operation is performed as:
- Let select index i = 2 and then A2 = 5 So that next element at Kth distance from i = (i + K) = (2+2) = 4, A4 = 8, Now if we jump more next Kth index, formally (4 + 2) = 6. So it doesn’t exist or we are out of the X[]. So the subsequence becomes = {5, 8}, Which gives sum as 13. Which is maximum possible.
Input: N = 4, K = 4, X[] = {1, 4, 5, 50}
Output: 50
Explanation: It will be optimal to choose i = 4 then A4 = 50. So the subsequence becomes {50}. Now, next (i+Kth) index doesn’t exist. Therefore the subsequence has sum as 50. Which is maximum possible. It can be verified that no sum more than 50 is possible using the given operation.
Approach: Implement the idea below to solve the problem
The problem is Greedy logic based and can be solved using the observations.
Steps were taken to solve the problem:
- Create an array of length N let’s say Y[].
- Create a variable let’s say max and initialize it to a minimum integer value for storing the maximum possible sum.
- Run a loop from i = N-1 to i ? 0 and follow the below-mentioned steps under the scope of the loop:
- Y[i] = i + K < N ? X[i] + Y[i + K] : X[i]
- Now just find the maximum value from Y[] by traversing it and updating the max
- Output the value of max.
Below is the code to implement the approach:
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void MaxSubsequenceSum( int N, int K, int X[]){
int l = INT_MIN;
int Y[N];
int sum = INT_MIN;
for ( int i = N - 1; i >= 0; i--) {
Y[i] = i + K < N ? X[i] + Y[i + K] : X[i];
}
for ( int i = 0; i < N; i++) {
sum = max(Y[i], sum);
}
cout<<sum<<endl;
}
int main() {
int N = 5;
int K = 2;
int X[] = { 2, 5, 5, 8, 2 };
MaxSubsequenceSum(N, K, X);
return 0;
}
|
Java
public class Main {
public static void main(String[] args)
{
int N = 5 ;
int K = 2 ;
int X[] = { 2 , 5 , 5 , 8 , 2 };
MaxSubsequenceSum(N, K, X);
}
static void MaxSubsequenceSum( int N, int K, int [] X)
{
int l = Integer.MIN_VALUE;
int Y[] = new int [N];
int max = Integer.MIN_VALUE;
for ( int i = N - 1 ; i >= 0 ; i--) {
Y[i] = i + K < N ? X[i] + Y[i + K] : X[i];
}
for ( int i = 0 ; i < N; i++) {
max = Math.max(Y[i], max);
}
System.out.println(max);
}
}
|
Python3
import sys
def MaxSubsequenceSum(N, K, X):
l = - sys.maxsize - 1
Y = [ 0 ] * N
sum = - sys.maxsize - 1
for i in range (N - 1 , - 1 , - 1 ):
Y[i] = X[i] + Y[i + K] if i + K < N else X[i]
for i in range (N):
sum = max (Y[i], sum )
print ( sum )
N = 5
K = 2
X = [ 2 , 5 , 5 , 8 , 2 ]
MaxSubsequenceSum(N, K, X)
|
C#
using System;
public class GFG {
static void MaxSubsequenceSum( int N, int K, int [] X)
{
int l = Int32.MinValue;
int [] Y = new int [N];
int max = Int32.MinValue;
for ( int i = N - 1; i >= 0; i--) {
Y[i] = i + K < N ? X[i] + Y[i + K] : X[i];
}
for ( int i = 0; i < N; i++) {
max = Math.Max(Y[i], max);
}
Console.WriteLine(max);
}
static public void Main()
{
int N = 5;
int K = 2;
int [] X = { 2, 5, 5, 8, 2 };
MaxSubsequenceSum(N, K, X);
}
}
|
Javascript
function MaxSubsequenceSum(N, K, X){
let l = Number.MIN_SAFE_INTEGER;
let Y = new Array(N);
let sum = Number.MIN_SAFE_INTEGER;
for (let i = N - 1; i >= 0; i--) {
Y[i] = i + K < N ? X[i] + Y[i + K] : X[i];
}
for (let i = 0; i < N; i++) {
sum = Math.max(Y[i], sum);
}
console.log(sum);
}
let N = 5;
let K = 2;
let X = [ 2, 5, 5, 8, 2 ];
MaxSubsequenceSum(N, K, X);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...