Count of pairs whose bitwise AND is a power of 2 | Java program for the above approach ; Method to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to return the number of valid pairs ; Iterate for all possible pairs ; Bitwise and value of the pair is passed ; Return the final count ; Driver Code ; Given array arr [ ] ; Function call
class GFG { static boolean check ( int x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } static int count ( int arr [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( check ( arr [ i ] & arr [ j ] ) ) cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 6 , 4 , 2 , 3 } ; int n = arr . length ; System . out . print ( count ( arr , n ) ) ; } }