Sum of Good Subsequences - Problem
You are given an integer array nums. A good subsequence is defined as a subsequence of nums where the absolute difference between any two consecutive elements in the subsequence is exactly 1.
Return the sum of all possible good subsequences of nums. Since the answer may be very large, return it modulo 10^9 + 7.
Note that a subsequence of size 1 is considered good by definition.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,1]
›
Output:
14
💡 Note:
Good subsequences: [1] (sum=1), [2] (sum=2), [1] (sum=1), [1,2] (sum=3), [2,1] (sum=3), [1,2,1] (sum=4). Total: 1+2+1+3+3+4 = 14
Example 2 — Single Element
$
Input:
nums = [3]
›
Output:
3
💡 Note:
Only one good subsequence: [3] with sum 3
Example 3 — No Extensions
$
Input:
nums = [1,3,5]
›
Output:
9
💡 Note:
No consecutive elements differ by exactly 1. Good subsequences are only single elements: [1], [3], [5]. Total: 1+3+5 = 9
Constraints
- 1 ≤ nums.length ≤ 105
- -105 ≤ nums[i] ≤ 105
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code