Given an array **arr[]** consisting of integers of length **N** and an integer **K** (1 <= K <= N), the task is to find the maximum sub-sequence sum in the array such that adjacent elements in that sub-sequence have at most a difference of **K** in their indices in the original array. In other words, if **i** and **j** are indices of two consecutive elements of sub-sequence in original array then **|i-j| <= K **.

**Examples:**

Input:arr[] = {1, 2, -2, 4, 3, 1}, K = 2Output:11–

Explanation :The subsequence with maximum sum is {1, 2, 4, 3, 1} (difference between indices <=2)

Input:arr[] = {4, -2, -2, -1, 3, -1}, K = 2Output:5

Explanation :The sub-sequence with maximum sum is {4, -2, 3} (difference between indices <=2)

**Naive approach:** Generate all possible subsets of the array and check for each of the subsets whether it satisfies the condition such that two adjacent elements have **at most 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.

Create a table** dp[]**, where **dp[i]** stores the largest possible sum for the sub-sequence till the **i ^{th}** index.

- If the current element is the first element of the sub-sequence then:

dp[i] =arr[i]

- Otherwise, we have to check previous results and check what is the maximum result of dp in a window of
**K**behind this index :

dp[i] = max(arr[i] + dp[x]) where x is from [i-k, 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(arr[i], arr[i] + dp[x]) where i-k <= x <= i-1

So, for checking what is the maximum value behind this index in a window of **K**, either we can iterate from dp[i-1] to dp[i-k] and find the maximum value, in which case the time complexity will be **O(N*K)** or it can be reduced by taking an ordered map and keep maintaining the computed dp[i] values for every index. This reduces the complexity to **O(N*log(K))**.

Below is the implementation of the above approach.

## C++

`// C++ implementation to ` `// C++ program to ` `// find the maximum sum ` `// subsequence such that ` `// two adjacent element ` `// have atmost difference ` `// of K in their indices ` ` ` `#include <iostream> ` `#include <iterator> ` `#include <map> ` `using` `namespace` `std; ` ` ` `int` `max_sum(` `int` `arr[], ` `int` `N, ` ` ` `int` `K) ` `{ ` ` ` ` ` `// DP Array to store the ` ` ` `// maximum sum obtained ` ` ` `// till now ` ` ` `int` `dp[N]; ` ` ` ` ` `// Ordered map to store DP ` ` ` `// values in a window ok K ` ` ` `map<` `int` `, ` `int` `> mp; ` ` ` ` ` `// Initializing dp[0]=arr[0] ` ` ` `dp[0] = arr[0]; ` ` ` ` ` `// Inserting value of DP[0] ` ` ` `// in map ` ` ` `mp[dp[0]]++; ` ` ` ` ` `// Intializing final answer ` ` ` `// with dp[0] ` ` ` `int` `ans = dp[0]; ` ` ` ` ` `for` `(` `int` `i = 1; ` ` ` `i < N; i++) { ` ` ` ` ` `// when i<k there is no ` ` ` `// need to delete elements ` ` ` `// from map as window is ` ` ` `// less than K ` ` ` `if` `(i < K) { ` ` ` ` ` `// Initializing interator ` ` ` `// to end af map ` ` ` `auto` `it = mp.end(); ` ` ` ` ` `// Decreasing iterator to ` ` ` `// get maximum key ` ` ` `it--; ` ` ` ` ` `// Evaluating DP[i] ` ` ` `// from reccurence ` ` ` `dp[i] = max(it->first ` ` ` `+ arr[i], ` ` ` `arr[i]); ` ` ` ` ` `// Inserting dp value in map ` ` ` `mp[dp[i]]++; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `auto` `it = mp.end(); ` ` ` `it--; ` ` ` `dp[i] = max(it->first ` ` ` `+ arr[i], ` ` ` `arr[i]); ` ` ` ` ` `mp[dp[i]]++; ` ` ` ` ` `// Deleting dp[i-k] from ` ` ` `// map beacuse window size ` ` ` `// has become K+1 ` ` ` `mp[dp[i - K]]--; ` ` ` ` ` `// Erase the key from if ` ` ` `// count of dp[i-K] becomes ` ` ` `// zero ` ` ` `if` `(mp[dp[i - K]] == 0) { ` ` ` ` ` `mp.erase(dp[i - K]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Calculating final answer ` ` ` `ans = max(ans, dp[i]); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, -2, ` ` ` `4, 3, 1 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` `int` `K = 2; ` ` ` `cout << max_sum(arr, N, K); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

**Output:**

11

**Time Complexity:** *O(N*log(K))*

## Recommended Posts:

- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum length subsequence with difference between adjacent elements as either 0 or 1
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
- 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
- Maximum sum such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Maximum set bit sum in array without considering adjacent elements
- 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
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Maximum subsequence sum such that all elements are K distance apart
- Maximum sum subsequence with at-least k distant elements
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Minimize the maximum difference of adjacent elements after at most K insertions

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.