You're given a 0-indexed integer array nums and need to find the maximum sum of a special type of subsequence called a balanced subsequence.
A subsequence with indices i₀ < i₁ < ... < iₖ₋₁ is balanced if for every consecutive pair of elements:
nums[iⱼ] - nums[iⱼ₋₁] ≥ iⱼ - iⱼ₋₁ for all j ∈ [1, k-1]
In simpler terms, the value difference between consecutive elements must be at least as large as their index difference. This ensures the subsequence grows "fast enough" relative to how spread out the indices are.
Goal: Return the maximum possible sum of elements in any balanced subsequence. Note that a single element is always considered balanced.
Example: For nums = [3, -1, 1, 2], the subsequence at indices [0, 3] gives us [3, 2]. Check: 2 - 3 = -1 and 3 - 0 = 3, so -1 ≥ 3 is false, making this not balanced.
Input & Output
Constraints
- 1 ≤ nums.length ≤ 105
- -109 ≤ nums[i] ≤ 109
- The subsequence must maintain original relative order of elements