-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountTriplets.cpp
More file actions
53 lines (38 loc) · 857 Bytes
/
CountTriplets.cpp
File metadata and controls
53 lines (38 loc) · 857 Bytes
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
// problem: "https://www.hackerrank.com/challenges/count-triplets-1/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps"
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll n,r;
cin>>n>>r;
ll i;
vector<ll>V(n);
for(i=0;i!=n;i++)
cin>>V[i];
map<ll,ll>M2;
for(i=0;i!=n;i++)
{
if(M2.count(V[i]))
M2[V[i]]++;
else
M2[V[i]] = 1;
}
map<ll,ll>M1;
ll c = 0;
for(i=0;i!=n;i++)
{
M2[V[i]]--;
if(V[i]%r==0)
{
if(M1.count(V[i]/r) && M2[V[i]*r]>0)
c += M1[V[i]/r] * M2[V[i]*r];
}
if(M1.count(V[i]))
M1[V[i]]++;
else
M1[V[i]] = 1;
}
cout<<c;
return 0;
}