fork download
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. // } Driver Code Ends
  7.  
  8. class Solution {
  9. public:
  10. long subarrayXor(vector<int> &arr, int k) {
  11. int n = arr.size();
  12. unordered_map<int, int> mp;
  13. int count = 0;
  14. int xorSum = 0;
  15.  
  16. for (int i = 0; i < n; i++) {
  17. xorSum ^= arr[i];
  18.  
  19. if (xorSum == k)
  20. count++;
  21.  
  22. if (mp.find(xorSum ^ k) != mp.end()) {
  23. count += mp[xorSum ^ k];
  24. }
  25.  
  26. mp[xorSum]++;
  27. }
  28.  
  29. return count;
  30. }
  31. };
  32.  
  33.  
  34.  
  35. //{ Driver Code Starts.
  36.  
  37. int main() {
  38. int tc;
  39. cin >> tc;
  40. cin.ignore(); // Ignore the newline after the number of test cases
  41.  
  42. while (tc-- > 0) {
  43. string input;
  44. getline(cin, input); // Read the whole line for the array
  45.  
  46. stringstream ss(input);
  47. vector<int> arr;
  48. int num;
  49.  
  50. while (ss >> num) {
  51. arr.push_back(num); // Push numbers into the vector
  52. }
  53.  
  54. int k;
  55. cin >> k;
  56. cin.ignore(); // Ignore the newline after k
  57.  
  58. Solution obj;
  59. cout << obj.subarrayXor(arr, k) << endl;
  60. cout << "~" << endl;
  61. }
  62. return 0;
  63. }
  64. // } Driver Code Ends
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty