Maximum Balanced Subsequence Sum - Problem
You are given a 0-indexed integer array nums. A subsequence of nums having length k and consisting of indices i₀ < i₁ < ... < iₖ₋₁ is balanced if the following holds:
nums[iⱼ] - nums[iⱼ₋₁] >= iⱼ - iⱼ₋₁, for every j in the range [1, k - 1].
A subsequence of nums having length 1 is considered balanced.
Return an integer denoting the maximum possible sum of elements in a balanced subsequence of nums.
A subsequence of an array is a new non-empty array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
Input & Output
Example 1 — Basic Balanced Subsequence
$
Input:
nums = [3,4,5,1]
›
Output:
12
💡 Note:
The balanced subsequence [3,4,5] at indices [0,1,2] gives maximum sum. Check: 4-3 ≥ 1-0 (1≥1 ✓), 5-4 ≥ 2-1 (1≥1 ✓). Sum = 3+4+5 = 12.
Example 2 — Single Element Maximum
$
Input:
nums = [-1,-2,-3]
›
Output:
-1
💡 Note:
All single elements are balanced. The maximum single element is -1, so return -1.
Example 3 — Non-consecutive Subsequence
$
Input:
nums = [5,-1,-3,8]
›
Output:
13
💡 Note:
The balanced subsequence [5,8] at indices [0,3] gives maximum sum. Check: 8-5 ≥ 3-0 (3≥3 ✓). Sum = 5+8 = 13.
Constraints
- 1 ≤ nums.length ≤ 105
- -109 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array [3,4,5,1] with indices [0,1,2,3]
2
Check Balance
For subsequence to be balanced: nums[j] - nums[j-1] ≥ j - (j-1)
3
Find Maximum
Among all balanced subsequences, return maximum sum
Key Takeaway
🎯 Key Insight: Transform the balance condition to nums[i] - i ≥ nums[j] - j for efficient DP solution
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code