Maximum sum subsequence with at-least k distant elements
Given an array and a number k, find a subsequence such that
- Sum of elements in subsequence is maximum
- Indices of elements of subsequence differ atleast by k
Examples
Input : arr[] = {4, 5, 8, 7, 5, 4, 3, 4, 6, 5}
k = 2
Output: 19
Explanation: The highest value is obtained
if you pick indices 1, 4, 7, 10 giving
4 + 7 + 3 + 5 = 19
Input: arr[] = {50, 70, 40, 50, 90, 70, 60,
40, 70, 50}
k = 2
Output: 230
Explanation: There are 10 elements and k = 2.
If you select 2, 5, and 9 you get a total
value of 230, which is the maximum possible.
A simple solution is to consider all subsequences one by one. In every subsequence, check for distance condition and return the maximum sum subsequence. An efficient solution is to use dynamic programming.
There are two cases:
- If we select element at index i such that i + k + 1 >= N, then we cannot select any other element as part of the subsequence. Hence we need to decide whether to select this element or one of the elements after it.
- If we select element at index i such that i + k + 1 < N, then the next element we can select is at i + k + 1 index. Thus we need to decide whether to select these two elements, or move on to the next adjacent element.
These two cases can be written as:
Let MS[i] denotes the maximum sum of subsequence
from i = N-2 to 0.
Base Case:
MS[N-1] = arr[N-1]
If i + 1 + k >= N
MS[i] = max(arr[i], MS[i+1]),
Else
MS[i] = max(arr[i] + MS[i+k+1], MS[i+1])
Evidently, the solution to the problem
is to find MS[0].
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum( int arr[], int N, int k)
{
int MS[N];
MS[N - 1] = arr[N - 1];
for ( int i = N - 2; i >= 0; i--) {
if (i + k + 1 >= N)
MS[i] = max(arr[i], MS[i + 1]);
else
MS[i] = max(arr[i] + MS[i + k + 1], MS[i + 1]);
}
return MS[0];
}
int main()
{
int N = 10, k = 2;
int arr[] = { 50, 70, 40, 50, 90, 70, 60, 40, 70, 50 };
cout << maxSum(arr, N, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxSum( int arr[], int N, int k)
{
int MS[] = new int [N];
MS[N - 1 ] = arr[N - 1 ];
for ( int i = N - 2 ; i >= 0 ; i--) {
if (i + k + 1 >= N)
MS[i] = Math.max(arr[i], MS[i + 1 ]);
else
MS[i] = Math.max(arr[i] + MS[i + k + 1 ],
MS[i + 1 ]);
}
return MS[ 0 ];
}
public static void main(String[] args)
{
int N = 10 , k = 2 ;
int arr[] = { 50 , 70 , 40 , 50 , 90 , 70 , 60 ,
40 , 70 , 50 };
System.out.println(maxSum(arr, N, k));
}
}
|
Python3
def maxSum(arr, N, k):
MS = [ 0 for i in range (N)]
MS[N - 1 ] = arr[N - 1 ]
for i in range (N - 2 , - 1 , - 1 ):
if (i + k + 1 > = N):
MS[i] = max (arr[i], MS[i + 1 ])
else :
MS[i] = max (arr[i] + MS[i + k + 1 ],
MS[i + 1 ])
return MS[ 0 ]
N = 10 ; k = 2
arr = [ 50 , 70 , 40 , 50 , 90 , 70 , 60 , 40 , 70 , 50 ]
print (maxSum(arr, N, k))
|
C#
using System;
class GFG {
static int maxSum( int []arr, int N, int k)
{
int []MS = new int [N];
MS[N - 1] = arr[N - 1];
for ( int i = N - 2; i >= 0; i--) {
if (i + k + 1 >= N)
MS[i] = Math.Max(arr[i], MS[i + 1]);
else
MS[i] = Math.Max(arr[i] + MS[i + k + 1],
MS[i + 1]);
}
return MS[0];
}
public static void Main()
{
int N = 10, k = 2;
int []arr = { 50, 70, 40, 50, 90, 70, 60,
40, 70, 50 };
Console.WriteLine(maxSum(arr, N, k));
}
}
|
PHP
<?php
function maxSum( $arr , $N , $k )
{
$MS [ $N - 1] = $arr [ $N - 1];
for ( $i = $N - 2; $i >= 0; $i --)
{
if ( $i + $k + 1 >= $N )
$MS [ $i ] = max( $arr [ $i ],
$MS [ $i + 1]);
else
$MS [ $i ] = max( $arr [ $i ] +
$MS [ $i + $k + 1],
$MS [ $i + 1]);
}
return $MS [0];
}
$N = 10; $k = 2;
$arr = array (50, 70, 40, 50, 90,
70, 60, 40, 70, 50);
echo (maxSum( $arr , $N , $k ));
?>
|
Javascript
<script>
function maxSum(arr, N, k)
{
let MS = [];
MS.length = N;
MS[N-1] = arr[N-1];
for (let i = N - 2; i >= 0; i--){
if (i+k+1 >= N){
MS[i] = Math.max(arr[i], MS[i+1]);
}
else {
MS[i] = Math.max(arr[i] + MS[i+k+1], MS[i+1]);
}
}
return MS[0];
}
let N = 10, k = 2;
let arr = [ 50, 70, 40, 50, 90, 70, 60, 40, 70, 50];
document.write(maxSum(arr, N, k));
</script>
|
Time Complexity : O(n) Auxiliary Space : O(n)
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...