We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm not 100% I understand everything that is going on when r=1 versus my code which got stuck on it. But for what it's worth, I found my translation of the code above a bit easier to consume for non-Python speakers. Works just as well.
C# ver:
long triplets = 0;
Dictionary<long, long> dict = new Dictionary<long, long>();
Dictionary<long, long> dictPairs = new Dictionary<long, long>();
for(int index = arr.Count-1; index >= 0; index--)
{
if (dictPairs.ContainsKey(arr[index]*r))
triplets += dictPairs[arr[index]*r];
if (dict.ContainsKey(arr[index]*r))
{
if(dictPairs.ContainsKey(arr[index]))
dictPairs[arr[index]] = dictPairs[arr[index]] + dict[arr[index]*r];
else
dictPairs[arr[index]] = dict[arr[index]*r];
}
if(!dict.ContainsKey(arr[index]))
dict.Add(arr[index], 1);
else
dict[arr[index]]++;
}
return triplets;
Cookie support is required to access HackerRank
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 →
I'm not 100% I understand everything that is going on when r=1 versus my code which got stuck on it. But for what it's worth, I found my translation of the code above a bit easier to consume for non-Python speakers. Works just as well.
C# ver: