[InterviewBit] Subarray with given XOR

Subarray with given XOR

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
int Solution::solve(vector<int> &A, int B) {
unordered_map<int, int> freq{{0,1}};
int res = 0, now = 0;
for(int i = 0; i < A.size(); i++) {
now ^= A[i];
res += freq[now ^ B];
freq[now] += 1;
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/03/PS/interviewbit/subarray-with-given-xor/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.