Count pairs in array such that one element is power of another | Function to check if given number number y is power of x ; log function to calculate value ; compare to the result1 or result2 both are equal ; Function to find pairs from array ; Iterate through all pairs ; Increment count if one is the power of other ; Driver code
function isPower ( x , y ) { var res1 = parseInt ( Math . log ( y ) / Math . log ( x ) ) ; var res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } function countPower ( arr , n ) { var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) if ( isPower ( arr [ i ] , arr [ j ] ) || isPower ( arr [ j ] , arr [ i ] ) ) res ++ ; return res + 1 ; } var a = [ 16 , 2 , 3 , 9 ] ; var n = a . length ; document . write ( countPower ( a , n ) ) ;