We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e9aeec0 commit 5b26d4eCopy full SHA for 5b26d4e
3583. Count Special Triplets
@@ -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