Ctrl K

Total of Smooth Picks

Hard
You get an integer array values. Call a subsequence (elements taken in their original order, not necessarily adjacent) **smooth** if every two neighbouring elements in it differ by exactly 1, up or down; the direction may change along the way. A one-element subsequence is always smooth. Subsequences taken from different positions count separately, even if they hold the same numbers. Return the sum of the elements of all smooth subsequences, modulo 10⁹ + 7.
Example 1
values=[2, 3, 2] → 24

The valid picks are [2], [3], [2], [2, 3], [3, 2] and [2, 3, 2]. Their sums: 2 + 3 + 2 + 5 + 5 + 7 = 24.

Example 2
values=[4, 6, 5] → 35

The valid picks are [4], [6], [5], [4, 5] and [6, 5]; [4, 6] does not count because the step is 2. The total is 15 + 9 + 11 = 35.

Example 3
values=[0, 0, 1] → 3
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Way to Space the Words HardSquare-Sum Chains HardSplit With a Repeat Penalty HardRoots That Fit the Guesses HardClimbing Along Rows and Columns Hard
loading Python…
Input
values=[2, 3, 2]
Expected
24

Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.