Count pairs whose products exist in array | Returns count of pairs whose product exists in arr ; Create an empty hash - set that store all array element ; Insert all array element into set ; Generate all pairs and check is exist in ' Hash ' or not ; if product exists in set then we increment count by 1 ; return count of pairs whose product exist in array ; Driver program
function countPairs ( arr , n ) { var result = 0 ; var Hash = new Set ( ) ; for ( i = 0 ; i < n ; i ++ ) { Hash . add ( arr [ i ] ) ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { var product = arr [ i ] * arr [ j ] ; if ( Hash . has ( product ) ) { result ++ ; } } } return result ; } var arr = [ 6 , 2 , 4 , 12 , 5 , 3 ] ; var n = arr . length ; document . write ( countPairs ( arr , n ) ) ;