fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int arr[] = {10, 5, 2, 7, 1, 9};
  6. int n = sizeof(arr) / sizeof(arr[0]);
  7. int k = 15;
  8. int pairs = 0;
  9.  
  10. unordered_map<int, int> mp;
  11. //if XOR(i…j) = k then P(i-1) = k XOR k = 0 so we store 0 already in map beforehand
  12. //specially for cases where valid subarray starts from index 0 itself
  13. mp[0] = 1; // prefix XOR of empty subarray is 0
  14.  
  15. int currentXor = 0;
  16. for (int j = 0; j < n; j++) {
  17. currentXor ^= arr[j];
  18. int toFind = currentXor ^ k;
  19. pairs += mp[toFind];
  20. mp[currentXor]++;
  21. }
  22. cout << pairs;
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
2