fork download
  1. // C++ program to Count all pair with given XOR
  2. // value x
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. // Returns count of pairs in arr[0..n-1] with XOR
  7. // value equals to x.
  8. int xorPairCount(int arr[], int n, int x)
  9. {
  10. int result = 0; // Initialize result
  11.  
  12. // create empty set that stores the visiting
  13. // element of array.
  14. // Refer below post for details of unordered_set
  15. // http://w...content-available-to-author-only...s.org/unorderd_set-stl-uses/
  16. unordered_set<int> s;
  17.  
  18. for (int i=0; i<n ; i++)
  19. {
  20. // If there exist an element in set s
  21. // with XOR equals to x^arr[i], that means
  22. // there exist an element such that the
  23. // XOR of element with arr[i] is equal to
  24. // x, then increment count.
  25. if (s.find(x^arr[i]) != s.end())
  26. result++;
  27.  
  28. // Make element visited
  29. s.insert(arr[i]);
  30. }
  31.  
  32. // return total count of pairs with XOR equal to x
  33. return result;
  34. }
  35.  
  36. // driver program
  37. int main()
  38. {
  39. int arr[] = {5 , 4 ,10, 15, 7, 6};
  40. int n = sizeof(arr)/sizeof(arr[0]);
  41. int x = 5;
  42. cout << "Count of pairs : "
  43. << xorPairCount(arr, n, x);
  44. return 0;
  45. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
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
Standard output is empty