// C++ program to Count all pair with given XOR
// value x
#include<bits/stdc++.h>
using namespace std;
// Returns count of pairs in arr[0..n-1] with XOR
// value equals to x.
int xorPairCount( int arr[ ] , int n, int x)
{
int result = 0 ; // Initialize result
// create empty set that stores the visiting
// element of array.
// Refer below post for details of unordered_set
// http://w...content-available-to-author-only...s.org/unorderd_set-stl-uses/
unordered_set< int > s;
for ( int i= 0 ; i< n ; i++ )
{
// 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.
if ( s.find ( x^ arr[ i] ) ! = s.end ( ) )
result++ ;
// Make element visited
s.insert ( arr[ i] ) ;
}
// return total count of pairs with XOR equal to x
return result;
}
// driver program
int main( )
{
int arr[ ] = { 5 , 4 ,10 , 15 , 7 , 6 } ;
int n = sizeof ( arr) / sizeof ( arr[ 0 ] ) ;
int x = 5 ;
cout << "Count of pairs : "
<< xorPairCount( arr, n, x) ;
return 0 ;
}
Ly8gQysrIHByb2dyYW0gdG8gQ291bnQgYWxsIHBhaXIgd2l0aCBnaXZlbiBYT1IKLy8gdmFsdWUgeAojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCi8vIFJldHVybnMgY291bnQgb2YgcGFpcnMgaW4gYXJyWzAuLm4tMV0gd2l0aCBYT1IKLy8gdmFsdWUgZXF1YWxzIHRvIHguCmludCB4b3JQYWlyQ291bnQoaW50IGFycltdLCBpbnQgbiwgaW50IHgpCnsKICAgIGludCByZXN1bHQgPSAwOyAvLyBJbml0aWFsaXplIHJlc3VsdAogCiAgICAvLyBjcmVhdGUgZW1wdHkgc2V0IHRoYXQgc3RvcmVzIHRoZSB2aXNpdGluZyAKICAgIC8vIGVsZW1lbnQgb2YgYXJyYXkuIAogICAgLy8gUmVmZXIgYmVsb3cgcG9zdCBmb3IgZGV0YWlscyBvZiB1bm9yZGVyZWRfc2V0CiAgICAvLyBodHRwOi8vdy4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4ucy5vcmcvdW5vcmRlcmRfc2V0LXN0bC11c2VzLwogICAgdW5vcmRlcmVkX3NldDxpbnQ+IHM7CiAKICAgIGZvciAoaW50IGk9MDsgaTxuIDsgaSsrKQogICAgewogICAgICAgIC8vIElmIHRoZXJlIGV4aXN0IGFuIGVsZW1lbnQgaW4gc2V0IHMKICAgICAgICAvLyB3aXRoIFhPUiBlcXVhbHMgdG8geF5hcnJbaV0sIHRoYXQgbWVhbnMKICAgICAgICAvLyB0aGVyZSBleGlzdCBhbiBlbGVtZW50IHN1Y2ggdGhhdCB0aGUKICAgICAgICAvLyBYT1Igb2YgZWxlbWVudCB3aXRoIGFycltpXSBpcyBlcXVhbCB0bwogICAgICAgIC8vIHgsIHRoZW4gaW5jcmVtZW50IGNvdW50LgogICAgICAgIGlmIChzLmZpbmQoeF5hcnJbaV0pICE9IHMuZW5kKCkpCiAgICAgICAgICAgIHJlc3VsdCsrOwogCiAgICAgICAgLy8gTWFrZSBlbGVtZW50IHZpc2l0ZWQKICAgICAgICBzLmluc2VydChhcnJbaV0pOwogICAgfQogCiAgICAvLyByZXR1cm4gdG90YWwgY291bnQgb2YgcGFpcnMgd2l0aCBYT1IgZXF1YWwgdG8geAogICAgcmV0dXJuIHJlc3VsdDsKfQogCi8vIGRyaXZlciBwcm9ncmFtCmludCBtYWluKCkKewogICAgaW50IGFycltdID0gezUgLCA0ICwxMCwgMTUsIDcsIDZ9OwogICAgaW50IG4gPSBzaXplb2YoYXJyKS9zaXplb2YoYXJyWzBdKTsKICAgIGludCB4ID0gNTsKICAgIGNvdXQgPDwgIkNvdW50IG9mIHBhaXJzIDogIgogICAgICAgICA8PCB4b3JQYWlyQ291bnQoYXJyLCBuLCB4KTsKICAgIHJldHVybiAwOwp9
compilation info
prog.cpp: In function 'int xorPairCount(int*, int, int)':
prog.cpp:16:5: error: 'unordered_set' was not declared in this scope
unordered_set<int> s;
^
prog.cpp:16:19: error: expected primary-expression before 'int'
unordered_set<int> s;
^
prog.cpp:25:13: error: 's' was not declared in this scope
if (s.find(x^arr[i]) != s.end())
^
prog.cpp:29:9: error: 's' was not declared in this scope
s.insert(arr[i]);
^
stdout