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 ; Driver Code ; Given array ; Size of the array ; Function call
def countPairs ( arr , N ) : NEW_LINE INDENT countPowerof2 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( bin ( arr [ i ] ) . count ( '1' ) == 1 ) : NEW_LINE INDENT countPowerof2 += 1 NEW_LINE DEDENT DEDENT desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) // 2 NEW_LINE print ( desiredPairs ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 7 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT