Count Triplets

  • + 0 comments

    Wow, that took a while. "The ratio of any two consecutive terms in a geometric progression is the same".

    def countTriplets(arr, r):
        ntuples = 0
        div_r = defaultdict(lambda: 0)
        r_val = defaultdict(lambda: 0)
        for v in arr:
            ntuples += div_r[v/r]
            div_r[v] += r_val[v/r]
            r_val[v] += 1
    
        return ntuples