Number of pairs whose product is a power of 2 | Function to count pairs having product equal to a power of 2 ; Stores count of array elements which are power of 2 ; If array element contains only one set bit ; Increase count of powers of 2 ; Count required number of pairs ; Print the required number of pairs ; Given array ; Size of the array ; Function Call
function countPairs ( arr , N ) { let countPowerof2 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( Number ( arr [ i ] . toString ( 2 ) . split ( " " ) . sort ( ) . join ( " " ) ) . toString ( ) . length == 1 ) countPowerof2 ++ ; } let desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; document . write ( desiredPairs + " " ) ; } let arr = [ 2 , 4 , 7 , 2 ] ; let N = arr . length ; countPairs ( arr , N ) ;