Count Triplets

  • + 0 comments

    curious to know what is the problem with below code?

    def countTriplets(arr, r):
        right = {}
        for elem in arr:
            right[elem] = right.get(elem, 0) + 1
        
        triplets = 0
        keys = []
        for item in right.keys():
            keys += [item]
        keys.sort()
        triplets = 0
        for index in range(len(keys) - 2):
            key_1 = right[keys[index]]
            key_2 = right[keys[index + 1]]
            key_3 = right[keys[index + 2]]
            lst = [key_1, key_2, key_3]
            lst.sort()
            if r == int(keys[index +1] / keys[index]) and r == int(keys[index +2] / keys[index + 1]) :
                triplets += key_1 * key_2 * key_3
                
        return triplets