[DAY5]Count Triplets

Sherlock Chiou
1 min readOct 14, 2019

--

https://www.hackerrank.com/challe…/count-triplets-1/problem…

Solution: Use two hash tables to record the number of (n) and (n, n*r). For each element in the array, it could be in the first, or second, or third position of the triplet. So, if an element with value x*r*r is in the r3 hash table. The triplets(x, x*r, x*r*r) is complete and we just renew the number of the triplets. Then, if the element is in the r2 hash table. We need to renew the number of (x*r, x*r*r) in r3 hash table by adding the r2[x*r*r]. Because previous (x*r) can all become (x*r, x*r*r) so we use add . Finally, the element could be in the first position, so the r2[x*r*r*r] += 1.

--

--

Sherlock Chiou
Sherlock Chiou

Written by Sherlock Chiou

Be Effective and Optimize for Learning

No responses yet