Given an array arr[] consisting of integers of length N and an integer K (1 ? k ? N), the task is to find the maximum subsequence sum in the array such that adjacent elements in that subsequence have at least a difference of K in their indices in the original array.
Examples:
Input: arr[] = {1, 2, -2, 4, 3, 1}, K = 4
Output: 4
Explanation:
Such Subsequences that can be selected: {1, 3}, {1, 1}, {2, 1}
Subsequence with maximum sum = (1 + 3) = 4
Selected elements are a[0] and a[4] (difference between indices = 4)
Input: arr[] = {1, 2, 72, 4, 3}, K = 2
Output: 76
Explanation:
Such Subsequences that can be selected – {{1, 72, 3}, {2, 4}, {2, 3}, {1, 4}, {1, 3}}
Subsequence with maximum sum = (1 + 72 + 3) = 76
Selected elements are a[0], a[2] and a[4] (difference between each indices = 2)
Naive approach: Generate all possible subsets of the array and check for each of the subsets that it satisfies the condition such that two adjacent elements have at least a difference of K in their indices. If yes, then compare its sum with the largest sum obtained till now and update the sum if it is greater than the obtained sum till now.
Efficient Approach: This problem can be solved using Dynamic Programming. In which for each element in array consider such that if we take this element then is it contributes into the final sum obtained If yes then add it into the DP array for that element.
Let’s decide the states of ‘dp’. Let dp[i] be the largest possible sum for the sub-array starting from index ‘0’ and ending at index ‘i’. Now, we have to find a recurrence relation between this state and a lower-order state.
Now the Recurrence Relation for the array will have two choices for every index i.
- Choose the current index:
In this case, the element that can be chosen is at index i-k.
So,
dp[i] = arr[i] + dp[i - k]
- Skip the current Index.
In this case, the element that can be chosen is at index i-1.
So,
dp[i] = dp[i - 1]
For every index choose that condition that gives the maximum sum at that index, So the final recurrence relation will be:
dp[i] = max(dp[i – 1], arr[i] + dp[i – k])
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maxi( int a, int b)
{
if (a > b) {
return a;
}
else {
return b;
}
}
int max_sum( int arr[], int n, int k)
{
int dp[n];
dp[0] = maxi(0, arr[0]);
int i = 1;
while (i < k) {
dp[i] = maxi(dp[i - 1], arr[i]);
i++;
}
i = k;
while (i < n) {
dp[i] = maxi(dp[i - 1], arr[i] + dp[i - k]);
i++;
}
return dp[n - 1];
}
int main()
{
int arr[] = { 1, 2, -2, 4, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 4;
cout << max_sum(arr, n, k);
return 0;
}
|
Java
class GFG
{
static int max_sum( int arr[], int n, int k)
{
int dp[] = new int [n];
dp[ 0 ] = Math.max( 0 , arr[ 0 ]);
int i = 1 ;
while (i < k)
{
dp[i] = Math.max(dp[i - 1 ], arr[i]);
i++;
}
i = k;
while (i < n)
{
dp[i] = Math.max(dp[i - 1 ],
arr[i] + dp[i - k]);
i++;
}
return dp[n - 1 ];
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , - 2 , 4 , 3 , 1 };
int n = arr.length;
int k = 4 ;
System.out.println(max_sum(arr, n, k));
}
}
|
Python3
def max_sum(arr, n, k) :
dp = [ 0 ] * n;
dp[ 0 ] = max ( 0 , arr[ 0 ]);
i = 1 ;
while (i < k) :
dp[i] = max (dp[i - 1 ], arr[i]);
i + = 1 ;
i = k;
while (i < n) :
dp[i] = max (dp[i - 1 ], arr[i] + dp[i - k]);
i + = 1 ;
return dp[n - 1 ];
if __name__ = = "__main__" :
arr = [ 1 , 2 , - 2 , 4 , 3 , 1 ];
n = len (arr)
k = 4 ;
print (max_sum(arr, n, k));
|
C#
using System;
class GFG
{
static int max_sum( int []arr, int n, int k)
{
int []dp = new int [n];
dp[0] = Math.Max(0, arr[0]);
int i = 1;
while (i < k)
{
dp[i] = Math.Max(dp[i - 1], arr[i]);
i++;
}
i = k;
while (i < n)
{
dp[i] = Math.Max(dp[i - 1],
arr[i] + dp[i - k]);
i++;
}
return dp[n - 1];
}
public static void Main()
{
int []arr = { 1, 2, -2, 4, 3, 1 };
int n = arr.Length;
int k = 4;
Console.WriteLine(max_sum(arr, n, k));
}
}
|
Javascript
<script>
function max_sum(arr , n , k)
{
var dp = Array.from({length: n}, (_, i) => 0);
dp[0] = Math.max(0, arr[0]);
var i = 1;
while (i < k)
{
dp[i] = Math.max(dp[i - 1], arr[i]);
i++;
}
i = k;
while (i < n)
{
dp[i] = Math.max(dp[i - 1],
arr[i] + dp[i - k]);
i++;
}
return dp[n - 1];
}
var arr = [ 1, 2, -2, 4, 3, 1 ];
var n = arr.length;
var k = 4;
document.write(max_sum(arr, n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)