Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | C # program to implement the above approach ; Function to count pairs that satisfy the above condition ; Stores the count of pairs ; Stores the count of array elements having same positions of MSB ; Traverse the array ; Stores the index of MSB of array elements ; Calculate number of pairs ; Driver Code ; Given Input ; Function call to count pairs satisfying the given condition
using System ; class GFG { static int cntPairs ( int [ ] arr , int N ) { int res = 0 ; int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int pos = ( int ) ( Math . Log ( arr [ i ] ) / Math . Log ( 2 ) ) ; bit [ pos ] ++ ; } for ( int i = 0 ; i < 32 ; i ++ ) { res += ( bit [ i ] * ( bit [ i ] - 1 ) ) / 2 ; } return res ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( cntPairs ( arr , N ) ) ; } }