Given an integer array **nums** and an integer **K**, The task is to find the maximum sum of a non-empty subsequence of the array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where** i < j**, the condition **j – i <= K** is satisfied.

A

subsequenceof an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.

**Examples:**

Input:nums = [10, 2, -10, 5, 20], K = 2Output:37Explanation:The subsequence is [10, 2, 5, 20].Input:nums = [-1, -2, -3], K = 1Output:-1Input:nums = [10, -2, -10, -5, 20], K = 2Output:23

**Aprroach:**

- The optimal solution of this problem can be achieved by using the Sliding Window Maximum .
- For every index, check what is the maximum value that can be obtained from a window of size K before it. If the maximum value is negative, use zero instead.

`// C++ program to find the maximum sum ` `// subsequence under given constraint ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function return the maximum sum ` `int` `ConstrainedSubsetSum(vector<` `int` `>& nums, ` ` ` `int` `k) ` `{ ` ` ` `deque<pair<` `int` `, ` `int` `> > d; ` ` ` ` ` `// Iterating the given array ` ` ` `for` `(` `int` `i = 0; i < nums.size(); i++) ` ` ` `{ ` ` ` `// Check if deque is empty ` ` ` `nums[i] += d.size() ` ` ` `? max(d.back().first, 0) : 0; ` ` ` ` ` `while` `(d.size() && ` ` ` `d.front().first < nums[i]) ` ` ` `d.pop_front(); ` ` ` ` ` `d.push_front({ nums[i], i }); ` ` ` ` ` `if` `(d.back().second == i - k) ` ` ` `d.pop_back(); ` ` ` `} ` ` ` ` ` `int` `ans = nums[0]; ` ` ` ` ` `for` `(` `auto` `x : nums) ` ` ` `ans = max(ans, x); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> nums = { 10, -2, -10, ` ` ` `-5, 20 }; ` ` ` `int` `K = 2; ` ` ` ` ` `// Function call ` ` ` `cout << ConstrainedSubsetSum(nums, K) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

23

**Time Complexity:** O(N)

**Space Complexity:** O(K)

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:

- Split the array into equal sum parts according to given conditions
- Find the last remaining Character in the Binary String according to the given conditions
- Maximize the Sum of a Subsequence from an Array based on given conditions
- Find the Maximum possible Sum for the given conditions
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Maximum Count of pairs having equal Sum based on the given conditions
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find the Maximum Alternate Subsequence Sum from a given array
- Maximum length sub-array which satisfies the given conditions
- Maximum length sub-array which satisfies the given conditions
- Maximum items that can be bought from the cost Array based on given conditions
- Maximum length Subsequence with alternating sign and maximum Sum
- Find the equal pairs of subsequence of S and subsequence of T
- Split N as the sum of K numbers satisfying the given conditions
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Range sum queries based on given conditions
- Find an array of size N that satisfies the given conditions
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Number of K's such that the given array can be divided into two sets satisfying the given conditions

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.