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.
defcountTriplets(arr,r):# Loop over the array and count valid tripletsn_triplets=0r2=r*rd={}d_r={}d_rr={}foriinrange(len(arr)):ifi>1andarr[i]%r2==0:# Total number of triples for this arr[i] = # any triples found up to this point + all instances of doubles below this point d_rr[arr[i]//r2] = d_rr.get(arr[i]//r2, 0) + d_r.get(arr[i]//r2, 0)ifi>0andarr[i]%r==0:# Total number of doubles for this arr[i] = # any doubles found up to this point + all instances of arr[i] below this point d_r[arr[i]//r] = d_r.get(arr[i]//r, 0) + d.get(arr[i]//r,0)d[arr[i]]=d.get(arr[i],0)+1forkind_rr:n_triplets+=d_rr[k]returnn_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 →
Solution in python ...