Skip to content

Commit babb0e0

Browse files
authored
Create 3583. Count Special Triplets (#954)
2 parents e9aeec0 + 5b26d4e commit babb0e0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

3583. Count Special Triplets

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int specialTriplets(vector<int>& nums) {
4+
const int n = 100001, mod = 1000000007;
5+
long ans = 0;
6+
int hash[n], prev[n];
7+
for(int i = 0; i < n; i++) hash[i] = prev[i] = 0;
8+
for(int i = 0; i < nums.size(); i++) hash[nums[i]]++;
9+
for(int i = 1; i < nums.size() - 1; i++) {
10+
prev[nums[i - 1]]++;
11+
int j = nums[i], k = 2*j;
12+
if(k < n) {
13+
ans += (long)prev[k] * (hash[k] - prev[k] - (j == 0));
14+
}
15+
}
16+
return ans % mod;
17+
}
18+
};

0 commit comments

Comments
 (0)