C
C#7mo ago
starshinata

Can't understand why this solution for a HackerRank problem is not working

Here is the link to the problem: https://www.hackerrank.com/challenges/count-triplets-1/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps Here is my solution: (fails for test cases 6 and 10)
private static long GetNumberOfTriplets(long[] array, long ratio)
{
var frequencyByInteger = array
.GroupBy(item => item)
.ToDictionary(g => g.Key, g => g.LongCount());

if (ratio == 1) return frequencyByInteger.Values.Sum(v => v * (v - 1) * (v - 2) / 6);

return array.Distinct()
.Where(v => frequencyByInteger.ContainsKey(v * ratio)
&& frequencyByInteger.ContainsKey(v * ratio * ratio))
.Sum(v => frequencyByInteger[v] * frequencyByInteger[v * ratio] * frequencyByInteger[v * ratio * ratio]);
}
private static long GetNumberOfTriplets(long[] array, long ratio)
{
var frequencyByInteger = array
.GroupBy(item => item)
.ToDictionary(g => g.Key, g => g.LongCount());

if (ratio == 1) return frequencyByInteger.Values.Sum(v => v * (v - 1) * (v - 2) / 6);

return array.Distinct()
.Where(v => frequencyByInteger.ContainsKey(v * ratio)
&& frequencyByInteger.ContainsKey(v * ratio * ratio))
.Sum(v => frequencyByInteger[v] * frequencyByInteger[v * ratio] * frequencyByInteger[v * ratio * ratio]);
}
I know there are a lot of solutions to this online, but I want to specifically understand why the above solution is not correct. My current assumption is that I've found something nobody else before me has found and it's a HackerRank bug, meaning my code is correct. Would be happy to be proven wrong.
HackerRank
Count Triplets | HackerRank
Return the count of triplets that form a geometric progression.
1 Reply
JakenVeina
JakenVeina7mo ago
why on earth would you think you've found a bug? does your code not actually fail for test cases 6 and 10, when you run it independently? what are test cases 6 and 10?