Count Number of Special Subsequences

A sequence is special if it consists of a positive number of 0s, followed by a positive number of 1s, then a positive number of 2s.

  • For example, [0,1,2] and [0,0,1,1,1,2] are special.
  • In contrast, [2,1,0], [1], and [0,1,2,0] are not special.

Given an array nums consisting of only integers 0, 1, and 2, return the number of different subsequences that are special. Since the answer may be very large, return it modulo 10^9 + 7.

A subsequence of an array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. Two subsequences are different if the set of indices chosen are different.

Example 1
Inputnums = [0,1,2,2]
Output3
The three special subsequences choose the single 0 and 1, then either the first 2, the second 2, or both 2s.
Example 2
Inputnums = [2,2,0,0]
Output0
There are no special subsequences in [2,2,0,0].

Constraints

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

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