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 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.

**Effiecient 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 staring 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 which gives the maximum sum at that index, So 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++

`// C++ implementation to find the ` `// maximum sum subsequence such that ` `// two adjacent element have atleast ` `// difference of K in their indices ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum ` `// between two elements a and b ` `int` `maxi(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a > b) { ` ` ` `return` `a; ` ` ` `} ` ` ` `else` `{ ` ` ` `return` `b; ` ` ` `} ` `} ` ` ` `// Function to find the maximum sum ` `// subsequence such that two adjacent ` `// element have atleast difference ` `// of K in their indices ` `int` `max_sum(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// DP Array to store the maximum ` ` ` `// sum obtained till now ` ` ` `int` `dp[n]; ` ` ` ` ` `// Either select the first element ` ` ` `// or Nothing ` ` ` `dp[0] = maxi(0, arr[0]); ` ` ` `int` `i = 1; ` ` ` ` ` `// Either Select the (i - 1) element ` ` ` `// or let the previous best answer be ` ` ` `// the current best answer ` ` ` `while` `(i < k) { ` ` ` `dp[i] = maxi(dp[i - 1], arr[i]); ` ` ` `i++; ` ` ` `} ` ` ` `i = k; ` ` ` ` ` `// Either select the best sum ` ` ` `// till previous_index or select the ` ` ` `// current element + best_sum till index-k ` ` ` `while` `(i < n) { ` ` ` `dp[i] = maxi(dp[i - 1], arr[i] + dp[i - k]); ` ` ` `i++; ` ` ` `} ` ` ` `return` `dp[n - 1]; ` `} ` ` ` `// Driver Code ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// maximum sum subsequence such that ` `// two adjacent element have atleast ` `// difference of K in their indices ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the maximum sum ` ` ` `// subsequence such that two adjacent ` ` ` `// element have atleast difference ` ` ` `// of K in their indices ` ` ` `static` `int` `max_sum(` `int` `arr[], ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// DP Array to store the maximum ` ` ` `// sum obtained till now ` ` ` `int` `dp[] = ` `new` `int` `[n]; ` ` ` ` ` `// Either select the first element ` ` ` `// or Nothing ` ` ` `dp[` `0` `] = Math.max(` `0` `, arr[` `0` `]); ` ` ` `int` `i = ` `1` `; ` ` ` ` ` `// Either Select the (i - 1) element ` ` ` `// or let the previous best answer be ` ` ` `// the current best answer ` ` ` `while` `(i < k) ` ` ` `{ ` ` ` `dp[i] = Math.max(dp[i - ` `1` `], arr[i]); ` ` ` `i++; ` ` ` `} ` ` ` `i = k; ` ` ` ` ` `// Either select the best sum ` ` ` `// till previous_index or select the ` ` ` `// current element + best_sum till index-k ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` `dp[i] = Math.max(dp[i - ` `1` `], ` ` ` `arr[i] + dp[i - k]); ` ` ` `i++; ` ` ` `} ` ` ` `return` `dp[n - ` `1` `]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `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)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# maximum sum subsequence such that ` `# two adjacent element have atleast ` `# difference of K in their indices ` ` ` `# Function to find the maximum sum ` `# subsequence such that two adjacent ` `# element have atleast difference ` `# of K in their indices ` `def` `max_sum(arr, n, k) : ` ` ` ` ` `# DP Array to store the maximum ` ` ` `# sum obtained till now ` ` ` `dp ` `=` `[` `0` `] ` `*` `n; ` ` ` ` ` `# Either select the first element ` ` ` `# or Nothing ` ` ` `dp[` `0` `] ` `=` `max` `(` `0` `, arr[` `0` `]); ` ` ` `i ` `=` `1` `; ` ` ` ` ` `# Either Select the (i - 1) element ` ` ` `# or let the previous best answer be ` ` ` `# the current best answer ` ` ` `while` `(i < k) : ` ` ` `dp[i] ` `=` `max` `(dp[i ` `-` `1` `], arr[i]); ` ` ` `i ` `+` `=` `1` `; ` ` ` ` ` `i ` `=` `k; ` ` ` ` ` `# Either select the best sum ` ` ` `# till previous_index or select the ` ` ` `# current element + best_sum till index-k ` ` ` `while` `(i < n) : ` ` ` `dp[i] ` `=` `max` `(dp[i ` `-` `1` `], arr[i] ` `+` `dp[i ` `-` `k]); ` ` ` `i ` `+` `=` `1` `; ` ` ` ` ` `return` `dp[n ` `-` `1` `]; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `-` `2` `, ` `4` `, ` `3` `, ` `1` `]; ` ` ` `n ` `=` `len` `(arr) ` ` ` `k ` `=` `4` `; ` ` ` `print` `(max_sum(arr, n, k)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// maximum sum subsequence such that ` `// two adjacent element have atleast ` `// difference of K in their indices ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the maximum sum ` ` ` `// subsequence such that two adjacent ` ` ` `// element have atleast difference ` ` ` `// of K in their indices ` ` ` `static` `int` `max_sum(` `int` `[]arr, ` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `// DP Array to store the maximum ` ` ` `// sum obtained till now ` ` ` `int` `[]dp = ` `new` `int` `[n]; ` ` ` ` ` `// Either select the first element ` ` ` `// or Nothing ` ` ` `dp[0] = Math.Max(0, arr[0]); ` ` ` `int` `i = 1; ` ` ` ` ` `// Either Select the (i - 1) element ` ` ` `// or let the previous best answer be ` ` ` `// the current best answer ` ` ` `while` `(i < k) ` ` ` `{ ` ` ` `dp[i] = Math.Max(dp[i - 1], arr[i]); ` ` ` `i++; ` ` ` `} ` ` ` `i = k; ` ` ` ` ` `// Either select the best sum ` ` ` `// till previous_index or select the ` ` ` `// current element + best_sum till index-k ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` `dp[i] = Math.Max(dp[i - 1], ` ` ` `arr[i] + dp[i - k]); ` ` ` `i++; ` ` ` `} ` ` ` `return` `dp[n - 1]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `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)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N)

**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Maximum length subsequence with difference between adjacent elements as either 0 or 1
- Maximum subsequence sum of at most K-distant adjacent elements
- Find the smallest subarray having atleast one duplicate
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Maximum subsequence sum possible by multiplying each element by its index
- Maximum average of a subarray of size of atleast X and atmost Y
- Check if every index i has an index j such that sum of elements in both directions are equal
- Number of permutations such that sum of elements at odd index and even index are equal
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Longest subsequence such that adjacent elements have at least one common digit
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.