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 got test 6 and 10 wrong. Any help will be appreciated!
Here is my code:
#!/bin/python3importmathimportosimportrandomimportreimportsys# Complete the countTriplets function below.defcountTriplets(arr,r):num_counts={}triplets=0# creating the hashmapforiinarr:ifiinnum_counts:num_counts[i]+=1else:num_counts[i]=1# counting trippletsforkeyinnum_counts:# if ar and ar^2 is not in the map skipa,ar,arr=0,0,0ifkey*rnotinnum_countsorkey*r**2notinnum_counts:continueifr==1:a=num_counts[key]triplets+=math.factorial(a)//(math.factorial(a-3)*6)else:a=num_counts[key]ar=num_counts[key*r]arr=num_counts[key*r**2]triplets+=a*ar*arrreturntripletsif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')nr=input().rstrip().split()n=int(nr[0])r=int(nr[1])arr=list(map(int,input().rstrip().split()))ans=countTriplets(arr,r)fptr.write(str(ans)+'\n')fptr.close()
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 got test 6 and 10 wrong. Any help will be appreciated!
Here is my code: