Count all distinct pairs with product equal to K | JavaScript 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
let MAX = 100000 ; function countPairsWithProductK ( arr , n , k ) { let count = 0 ; let i ; let hashmap = new Array ( MAX ) ; for ( i = 0 ; i < n ; i ++ ) hashmap [ arr [ i ] ] = true ; for ( i = 0 ; i < n ; i ++ ) { let x = arr [ i ] ; let index = 1.0 * k / arr [ i ] ; if ( index >= 0 && ( ( index - Math . floor ( index ) ) == 0 ) && hashmap [ k / x ] ) count ++ ; hashmap [ x ] = false ; } return count ; } let arr = [ 1 , 5 , 3 , 4 , 2 ] ; let N = arr . length ; let K = 3 ; document . write ( countPairsWithProductK ( arr , N , K ) ) ;