Count all pairs with given XOR | C ++ program to Count all pair with given XOR value x ; Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty set that stores the visiting element of array . Refer below post for details of unordered_set https : www . geeksforgeeks . org / unorderd_set - stl - uses / ; If there exist an element in set s with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Make element visited ; return total count of pairs with XOR equal to x ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorPairCount ( int arr [ ] , int n , int x ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( x ^ arr [ i ] ) != s . end ( ) ) result ++ ; s . insert ( arr [ i ] ) ; } return result ; } int main ( ) { int arr [ ] = { 5 , 4 , 10 , 15 , 7 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 5 ; cout << " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " << xorPairCount ( arr , n , x ) ; return 0 ; }