Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Constrained Subsequence Sum

Hard

Topics
ArrayDynamic ProgrammingQueueSliding WindowHeapMonotonic Queue

Given an integer array nums and an integer target (= k), return the maximum sum of a non-empty subsequence such that for every two consecutive chosen elements at indices i < j, the condition j - i <= target holds.

Example 1

Input:  nums = [10,2,-10,5,20], target = 2
Output: 37

Example 2

Input:  nums = [-1,-2,-3], target = 1
Output: -1

Example 3

Input:  nums = [10,-2,-10,-5,20], target = 2
Output: 23

Constraints

  • 1 <= target <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access