101 Logo
onenoughtone

Code Implementation

Special Subsequence Counter Implementation

Below is the implementation of the special subsequence counter:

solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Count the number of special subsequences in the array.
*
* @param {number[]} nums - The input array containing only 0s, 1s, and 2s
* @return {number} - The number of special subsequences
*/
function countSpecialSubsequences(nums) {
let count0 = 0;
let count1 = 0;
let count2 = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 0) {
// Double the existing count0 and add 1 for the new subsequence
count0 = 2 * count0 + 1;
} else if (nums[i] === 1) {
// Double the existing count1 and add count0 for new subsequences
count1 = 2 * count1 + count0;
} else if (nums[i] === 2) {
// Double the existing count2 and add count1 for new subsequences
count2 = 2 * count2 + count1;
}
}
return count2;
}
/**
* Count the number of special subsequences in the array with modular arithmetic.
*
* @param {number[]} nums - The input array containing only 0s, 1s, and 2s
* @return {number} - The number of special subsequences
*/
function countSpecialSubsequencesMod(nums) {
const MOD = 1000000007;
let count0 = 0;
let count1 = 0;
let count2 = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 0) {
count0 = (2 * count0 + 1) % MOD;
} else if (nums[i] === 1) {
count1 = (2 * count1 + count0) % MOD;
} else if (nums[i] === 2) {
count2 = (2 * count2 + count1) % MOD;
}
}
return count2;
}
/**
* Count the number of special subsequences in the array using an array-based approach.
*
* @param {number[]} nums - The input array containing only 0s, 1s, and 2s
* @return {number} - The number of special subsequences
*/
function countSpecialSubsequencesArray(nums) {
const MOD = 1000000007;
const dp = [0, 0, 0]; // dp[i] = number of subsequences ending with i
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
dp[num] = (2 * dp[num] + (num === 0 ? 1 : dp[num - 1])) % MOD;
}
return dp[2];
}
// Test cases
console.log(countSpecialSubsequences([0, 1, 2, 2])); // 3
console.log(countSpecialSubsequences([2, 2, 0, 0])); // 0
console.log(countSpecialSubsequences([0, 1, 2, 0, 1, 2])); // 7
console.log(countSpecialSubsequencesMod([0, 1, 2, 2])); // 3
console.log(countSpecialSubsequencesMod([2, 2, 0, 0])); // 0
console.log(countSpecialSubsequencesMod([0, 1, 2, 0, 1, 2])); // 7
console.log(countSpecialSubsequencesArray([0, 1, 2, 2])); // 3
console.log(countSpecialSubsequencesArray([2, 2, 0, 0])); // 0
console.log(countSpecialSubsequencesArray([0, 1, 2, 0, 1, 2])); // 7

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: First, understand that we need to count the number of special subsequences consisting of only 0s, followed by only 1s, followed by only 2s.
  2. Identify Key Insight: Recognize that we can use dynamic programming to keep track of the number of subsequences ending with 0, 1, and 2 at each position.
  3. Initialize Counters: Initialize three counters: count0, count1, and count2, to keep track of the number of subsequences ending with 0, 1, and 2, respectively.
  4. Update Counters: For each element in the array, update the appropriate counter based on its value (0, 1, or 2).
  5. Apply Modular Arithmetic: Since the answer may be very large, apply modular arithmetic to avoid overflow.
ProblemSolutionCode
101 Logo
onenoughtone