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 with difference between adjacent elements as either 0 or 1
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum subsequence sum of at most K-distant adjacent elements
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Minimize the maximum difference of adjacent elements after at most K insertions
- Minimize the maximum difference between adjacent elements in an array
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- 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
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Maximum sum such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum set bit sum in array without considering adjacent elements
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Maximum sum in circular array such that no two elements are adjacent

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.