Count pairs with Bitwise XOR greater than both the elements of the pair | Java program for the above approach ; Function to count pairs whose XOR is greater than the pair itself ; Stores the count of pairs ; Sort the array ; Traverse the array ; If current element is 0 , then ignore it ; Traverse all the bits of element A [ i ] ; If current bit is set then update the count ; Update bits [ ] at the most significant bit of A [ i ] ; Print the count ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; Arrays . sort ( A ) ; int [ ] bits = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 0 ) { continue ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( ( 1 << j ) & A [ i ] ) == 0 ) { count += bits [ j ] ; } } ++ bits [ ( int ) ( ( int ) ( Math . log ( A [ i ] ) / Math . log ( 2 ) ) ) ] ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . length ; countPairs ( arr , N ) ; } }