Sum of Good Subsequences

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.

Example 1
Inputnums = [1,2,1]
Output14
The good subsequences are [1], [2], [1], [1,2], [2,1], and [1,2,1], and the sum of their elements is 14.
Example 2
Inputnums = [3,4,5]
Output40
The good subsequences are [3], [4], [5], [3,4], [4,5], and [3,4,5], and the sum of their elements is 40.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate