Count Triplets

  • + 0 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