Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | Java program for the above approach ; Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Initializing the map M ; Populating the map ; Count number of pairs for every element in map using mathematical concept of combination ; As nC2 = n * ( n - 1 ) / 2 ; Return the resultant count ; Driver Code
import java . util . * ; class GFG { static int countOfPairs ( int [ ] arr , int N , int X ) { int count = 0 ; HashMap < Integer , Integer > M = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( M . containsKey ( arr [ i ] & X ) ) M . put ( ( arr [ i ] & X ) , M . get ( arr [ i ] & X ) + 1 ) ; else M . put ( arr [ i ] & X , 1 ) ; } for ( Integer entry : M . keySet ( ) ) { int p = M . get ( entry ) ; count += p * ( p - 1 ) / 2 ; } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 5 , 4 , 6 , 7 } ; int X = 6 ; int N = arr . length ; System . out . print ( countOfPairs ( arr , N , X ) ) ; } }