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.
Here is my solution but I still fail two test cases, does anyone have any recommendation to fix it, many thanks!
def countTriplets(arr, r):
dict={}
for i in arr:
if not dict.get(i):
dict[i]=[i*r,1]
else:
dict[i][1]+=1
temp=list(dict.keys())
print (temp)
print (dict)
result=0
for i in temp:
if r==1:
result+=dict.get(i)[1]*(dict.get(i)[1]-1)*(dict.get(i)[1]-2)/6
elif dict.get(dict.get(i)[0])!=None and dict.get(dict.get(dict.get(i)[0])[0])!=None:
first=i
second=dict.get(first)[0]
third=dict.get(second)[0]
result+=dict.get(first)[1]*dict.get(second)[1]*dict.get(third)[1]
return int(result)
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 →
Here is my solution but I still fail two test cases, does anyone have any recommendation to fix it, many thanks!