Count Triplets

  • + 0 comments
    def countTriplets(arr, r):
        seconds = {}
        thirds = {}
        count = 0
    
        for i in arr:
            
            if i in thirds:
                count += thirds[i]
    
            if i in seconds:
                if i * r in thirds:
                    thirds[i * r] += seconds[i]
                else:
                    thirds[i * r] = seconds[i]
    
            if i * r in seconds:
                seconds[i * r] += 1
            else:
                seconds[i * r] = 1
    
        return count