Constrained Subsequence Sum

You're given an integer array nums and an integer k. Your task is to find the maximum sum of a non-empty subsequence where consecutive elements in the subsequence have a special constraint.

๐ŸŽฏ The Constraint: For any two consecutive integers in your chosen subsequence, nums[i] and nums[j] where i < j, the condition j - i <= k must be satisfied.

What's a subsequence? It's obtained by deleting some elements (possibly zero) from the original array while keeping the remaining elements in their original relative order.

Example: If nums = [10,2,-10,5,20] and k = 2, you could choose subsequence [10, 5, 20] (indices 0, 3, 4) because the gaps are 3-0=3 > 2... wait, that violates the constraint! You need gaps โ‰ค k between consecutive picks.

Input & Output

example_1.py โ€” Basic case
$ Input: nums = [10,2,-10,5,20], k = 2
โ€บ Output: 37
๐Ÿ’ก Note: The optimal subsequence is [10, 5, 20]. We can choose index 0 (value 10), then index 3 (value 5) since 3-0=3 > 2, this violates constraint. Actually, we need to check: we can go 0โ†’3 if 3-0 โ‰ค 2? No. So we try 0โ†’1โ†’3โ†’4: gaps are 1,2,1 all โ‰ค 2. Sum = 10+2+5+20=37.
example_2.py โ€” Negative values
$ Input: nums = [-1,-2,-3], k = 1
โ€บ Output: -1
๐Ÿ’ก Note: All values are negative, so we must choose at least one element. The best choice is the largest (least negative) value, which is -1.
example_3.py โ€” Large gap constraint
$ Input: nums = [10,-2,5,6], k = 3
โ€บ Output: 21
๐Ÿ’ก Note: With k=3, we can jump at most 3 positions. The optimal subsequence is [10, 5, 6] with indices [0, 2, 3]. Gaps are 2-0=2 โ‰ค 3 and 3-2=1 โ‰ค 3. Sum = 10+5+6=21.

Visualization

Tap to expand
๐Ÿƒโ€โ™‚๏ธ Strategic Runner: Constrained Subsequence Sum10Station 02Station 1-10Station 25Station 320Station 4k = 2 (max jump distance)Dynamic Programming Solutiondp[0] = 10 (start here)dp[1] = max(2, 2 + dp[0]) = max(2, 12) = 12dp[2] = max(-10, -10 + max(dp[0], dp[1])) = max(-10, 2) = 2dp[3] = max(5, 5 + max(dp[1], dp[2])) = max(5, 17) = 17dp[4] = max(20, 20 + max(dp[2], dp[3])) = max(20, 37) = 37Maximum Sum = 37 โœจ๐ŸŽฏ Optimal Path: Stations 0 โ†’ 3 โ†’ 4Jump distances: 3-0=3 > k? Wait... that's wrong!Actually: 0 โ†’ 1 โ†’ 3 โ†’ 4 (jumps: 1, 2, 1 all โ‰ค 2) โœ“Total reward: 10 + 2 + 5 + 20 = 37
Understanding the Visualization
1
Setup the Path
Each array position is a station with a reward/penalty value
2
Apply the Constraint
Runner can only jump at most k stations ahead (j - i โ‰ค k)
3
Use Smart Memory
At each station, remember the best total reward from previous k stations
4
Make Optimal Choice
Either start fresh at current station or add current reward to best previous total
Key Takeaway
๐ŸŽฏ Key Insight: Use a monotonic deque to efficiently track the maximum DP value in a sliding window of size k, enabling O(n) time complexity instead of O(nk).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is added and removed from deque at most once, making it O(n) amortized

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

DP array takes O(n) space, deque stores at most k elements

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • -104 โ‰ค nums[i] โ‰ค 104
  • 1 โ‰ค k โ‰ค nums.length
  • The subsequence must be non-empty
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
67.8K Views
High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen