-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountTriplets.cpp
More file actions
31 lines (26 loc) · 901 Bytes
/
CountTriplets.cpp
File metadata and controls
31 lines (26 loc) · 901 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
/*
https://www.hackerrank.com/challenges/count-triplets-1/problem
Count triplets solution from the problem above.
Simply replace the function given with the one below and run it for correctness.
*/
long countTriplets(vector<long> arr, long r) {
map<int,long> mp2, mp3;
//mp2 to hold count of needed values after this one to complete
//2nd part of triplet
//mp3 to hold count of needed values to complete triplet
int val;
int n = arr.size();
long long res = 0;
int i = 0;
while(n--)
{
val = arr[i];
if (mp3.count(val)) //This value completes mp3[val] triplets
res += mp3[val];
if (mp2.count(val)) //This value is valid as 2° part of mp2[val] triplets
mp3[val*r] += mp2[val];
mp2[val*r]++; //"Push-up" this value as possible triplet start
i++;
}
return res;
}