You are viewing a single comment's thread. Return to all comments →
Clears all testcases. Could have also used TryGetValue and save some ifs
static long countTriplets(List<long> arr, long r) { var mp2 = new System.Collections.Generic.Dictionary<long,long>(); var mp3 = new System.Collections.Generic.Dictionary<long,long>(); long res = 0; foreach (long val in arr) { if (mp3.ContainsKey(val)) res += mp3[val]; if (mp2.ContainsKey(val)) if (mp3.ContainsKey(val*r)) mp3[val*r] += mp2[val]; else mp3[val*r] = mp2[val]; if (mp2.ContainsKey(val*r)) mp2[val*r]++; else mp2[val*r] = 1; } return res; }
Seems like cookies are disabled on this browser, please enable them to open this website
Count Triplets
You are viewing a single comment's thread. Return to all comments →
Clears all testcases. Could have also used TryGetValue and save some ifs