Number of pairs such that their HCF and LCM is equal | Function to return HCF of two numbers ; Function to return LCM of two numbers ; Returns the number of valid pairs ; Traversing the array . For each array element , checking if it follow the condition ; Driver function
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function lcm ( a , b ) { return ( a * b ) / gcd ( a , b ) ; } function countPairs ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) ans ++ ; return ans ; } var arr = [ 1 , 1 , 1 ] ; var n = arr . length ; document . write ( countPairs ( arr , n ) ) ;