Count all distinct pairs with product equal to K | Python3 program to count the number of pairs whose product is equal to K ; Function to count the number of pairs whose product is equal to K ; Initialize the count ; Initialize empty hashmap . ; Insert array elements to hashmap ; Checking if the index is a whole number and present in the hashmap ; Driver code
MAX = 100000 ; NEW_LINE def countPairsWithProductK ( arr , n , k ) : NEW_LINE INDENT count = 0 ; NEW_LINE hashmap = [ False ] * MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT hashmap [ arr [ i ] ] = True ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT x = arr [ i ] ; NEW_LINE index = 1.0 * k / arr [ i ] ; NEW_LINE if ( index >= 0 and ( ( index - int ( index ) ) == 0 ) and hashmap [ k // x ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT hashmap [ x ] = False ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 3 , 4 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 3 ; NEW_LINE print ( countPairsWithProductK ( arr , N , K ) ) ; NEW_LINE DEDENT