Count unordered pairs ( i , j ) such that product of a [ i ] and a [ j ] is power of two | C ++ program to Count unordered pairs ( i , j ) in array such that product of a [ i ] and a [ j ] can be expressed as power of two ; Function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to Count unordered pairs ; is a number can be expressed as power of two ; count total number of unordered pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int x ) { return x && ( ! ( x & ( x - 1 ) ) ) ; } void Count_pairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) ) count ++ ; } int ans = ( count * ( count - 1 ) ) / 2 ; cout << ans << " STRNEWLINE " ; } int main ( ) { int a [ ] = { 2 , 5 , 8 , 16 , 128 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; Count_pairs ( a , n ) ; return 0 ; }