Skip to content

Practice Sum of Good Subsequences

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3351
LeetCode Problem

Sum of Good Subsequences

Problem Statement

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.

Source: Sum of Good Subsequences on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,1]
Output
14

# Example 2

Input
nums = [3,4,5]
Output
40

Constraints

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

Related LeetCode Problems

Practice more problems similar to Sum of Good Subsequences.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.