Count all distinct pairs with product equal to K | Java 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
class GFG { static int MAX = 100000 ; static int countPairsWithProductK ( int arr [ ] , int n , int k ) { int count = 0 ; int i ; boolean hashmap [ ] = new boolean [ MAX ] ; for ( i = 0 ; i < n ; i ++ ) hashmap [ arr [ i ] ] = true ; for ( i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; double index = 1.0 * k / arr [ i ] ; if ( index >= 0 && ( ( index - ( int ) ( index ) ) == 0 ) && hashmap [ k / x ] ) count ++ ; hashmap [ x ] = false ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int N = arr . length ; int K = 3 ; System . out . print ( countPairsWithProductK ( arr , N , K ) ) ; } }