Maximum subsequence sum such that no K elements are consecutive
Last Updated :
26 May, 2021
Given an array arr[] of N positive integers, the task is to find the maximum sum of a subsequence consisting of no K consecutive array elements.
Examples:
Input: arr[] = {10, 5, 8, 16, 21}, K = 4Output: 55Explanation: Maximum sum is obtained by picking 10, 8, 16, 21.
Input: arr[] = {4, 12, 22, 18, 34, 12, 25}, K = 5Output: 111Explanation: Maximum sum is obtained by picking 12, 22, 18, 34, 25
Naive Approach: The simplest approach is to generate all the subsets of the given array and for each subset, check if it contains K consecutive array elements or not. For subsets found to be not containing K consecutive array elements, calculate their sum. Find the maximum of the sums of all such subsequences.
Time Complexity: O(N*2N )Auxiliary Space: O(1)
Efficient Approach: There are many overlapping subproblems in the above solution which are calculated again and again. To avoid recomputation of the same subproblems, the idea is to use Memoization or Tabulation . Follow the steps below to solve the problem:
Initialize an array dp[] to memorize the maximum value of the sum up to each index.
Now, dp[i] gives the maximum value of the sum that can be picked such that no K elements are consecutive from the 0th Index till ith index.
The base case is when i < K :
Since the array elements are all positive, pick all the elements before (K )th Index.
So dp[1] = arr [0] and dp[i] = dp[i -1] + arr[i-1], (1 ≤ i < k ).
Now for i ≥ K :
Since K consecutive elements cannot be picked, so skip at least one element from i to (i – K + 1) inclusive so to make sure that no K elements are consecutive.
Since any element can contribute to the result, so skip every element from i to (i – K + 1) inclusive and will keep track of the maximum sum.
To skip the jth Element, add maximum sum till (j – 1)th Index which is given by dp[j – 1] with the sum of all the elements from (j + 1)th index to ith index which can be calculated in O(1) time using prefix array sum .
Therefore update the current dp state as: dp[i] = max (dp[i], dp[j -1] + prefix[i] – prefix [j]), (i ≤ j ≤ (i – K + 1)) , where prefix array stores the prefix sum.
Print the maximum sum after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Max_Sum(int arr[], int K, int N)
{
int dp[N + 1];
memset(dp, 0, sizeof(dp));
int prefix[N + 1];
prefix[0] = 0;
for(int i = 1; i <= N; i++)
{
prefix[i] = prefix[i - 1] + arr[i-1];
}
dp[0] = 0;
for(int i = 1; i < K ; i++)
{
dp[i] = prefix[i];
}
for(int i = K ; i <= N; ++i)
{
for(int j = i; j >= (i - K + 1); j--)
{
dp[i] = max(dp[i], dp[j - 1] +
prefix[i] - prefix[j]);
}
}
return dp[N];
}
int main()
{
int arr[] = { 4, 12, 22, 18, 34, 12, 25 };
int N = sizeof(arr) / sizeof(int);
int K = 5;
cout << Max_Sum(arr, K, N);
return 0;
}
Java
import java.io.*;
import java.util.*;
class GFG{
public static int Max_Sum(int[] arr, int K,
int N)
{
int[] dp = new int[N + 1];
Arrays.fill(dp, 0);
int[] prefix = new int[N + 1];
prefix[0] = 0;
for(int i = 1; i <= N; i++)
{
prefix[i] = prefix[i - 1] + arr[i-1];
}
dp[0] = 0;
for(int i = 1; i <= K - 1; i++)
{
dp[i] = prefix[i];
}
for(int i = K ; i <= N; ++i)
{
for(int j = i; j >= (i - K + 1); j--)
{
dp[i] = Math.max(dp[i], dp[j - 1] +
prefix[i] - prefix[j]);
}
}
return dp[N];
}
public static void main(String[] args)
{
int[] arr = { 4, 12, 22, 18, 34, 12, 25 };
int N = arr.length;
int K = 5;
System.out.println(Max_Sum(arr, K, N));
}
}
Python3
def Max_Sum(arr, K, N):
dp = [0] * (N + 1)
prefix = [None] * (N + 1)
prefix[0] = 0
for i in range(1, N + 1):
prefix[i] = prefix[i - 1] + arr[i - 1]
dp[0] = 0
for i in range(1, K):
dp[i] = prefix[i]
for i in range(K, N + 1):
for j in range(i, i - K, -1):
dp[i] = max(dp[i], dp[j - 1] +
prefix[i] - prefix[j])
return dp[N]
if __name__ == "__main__":
arr = [ 4, 12, 22, 18, 34, 12, 25 ]
N = len(arr)
K = 5
print(Max_Sum(arr, K, N))
C#
using System;
class GFG{
static int Max_Sum(int[] arr, int K, int N)
{
int[] dp = new int[N + 1];
Array.Fill(dp, 0);
int[] prefix = new int[N + 1];
prefix[0] = 0;
for(int i = 1; i <= N; i++)
{
prefix[i] = prefix[i - 1] + arr[i - 1];
}
dp[0] = 0;
for(int i = 1; i <= K - 1; i++)
{
dp[i] = prefix[i];
}
for(int i = K; i <= N; ++i)
{
for(int j = i; j >= (i - K + 1); j--)
{
dp[i] = Math.Max(dp[i], dp[j - 1] +
prefix[i] - prefix[j]);
}
}
return dp[N];
}
static public void Main()
{
int[] arr = { 4, 12, 22, 18, 34, 12, 25 };
int N = arr.Length;
int K = 5;
Console.WriteLine(Max_Sum(arr, K, N));
}
}
Javascript
<script>
function Max_Sum(arr, K, N)
{
var dp = Array(N+1).fill(0);
var prefix = Array(N+1);
prefix[0] = 0;
for(var i = 1; i <= N; i++)
{
prefix[i] = prefix[i - 1] + arr[i-1];
}
dp[0] = 0;
for(var i = 1; i < K ; i++)
{
dp[i] = prefix[i];
}
for(var i = K ; i <= N; ++i)
{
for(var j = i; j >= (i - K + 1); j--)
{
dp[i] = Math.max(dp[i], dp[j - 1] +
prefix[i] - prefix[j]);
}
}
return dp[N];
}
var arr = [4, 12, 22, 18, 34, 12, 25];
var N = arr.length;
var K = 5;
document.write( Max_Sum(arr, K, N));
</script>
Time Complexity: O(N*K) where N is the number of elements in the array and K is the input such that no K elements are consecutive.Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...