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.
First we import defaultdictionary from collections module. Then, use this code for better Understanding:
def countTriplets(arr, r):
total_pairs = 0
count2={}
count3={}
for num in arr:
if num in count3:
total_pairs+=count3[num]
if num in count2:
count3[num*r]=count3.get(num*r, 0) + count2[num]
count2[num*r]=count2.get(num*r, 0) + 1
return total_pairs
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 →
First we import defaultdictionary from collections module. Then, use this code for better Understanding: def countTriplets(arr, r): total_pairs = 0 count2={} count3={} for num in arr: if num in count3: total_pairs+=count3[num] if num in count2: count3[num*r]=count3.get(num*r, 0) + count2[num] count2[num*r]=count2.get(num*r, 0) + 1 return total_pairs