Skip to content

Instantly share code, notes, and snippets.

@Nimishkhurana
Last active August 6, 2019 16:33
Show Gist options
  • Save Nimishkhurana/65c22b14c8225bef00b37953a620c4a1 to your computer and use it in GitHub Desktop.
Save Nimishkhurana/65c22b14c8225bef00b37953a620c4a1 to your computer and use it in GitHub Desktop.
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int findSubarrCnt(int arr[], int n)
{
int ans = 0, XOR = 0;
// Array to store prefix XOR's
int prefix[n];
for (int i = 0; i < n; ++i) {
// Calculate XOR until this index
XOR = XOR ^ arr[i];
// Store the XOR in prefix array
prefix[i] = XOR;
// cout<<"Prefix at "<<i<<" ="<<XOR<<endl;
}
unordered_map<int, vector<int>> group;
for (int i = 0; i < n; ++i) {
group[prefix[i]].push_back(i);
}
for(auto x: group){
// cout<<endl<<"Prefix = "<<x.first<<endl;
vector<int> temp = x.second;
if(x.first==0)
ans+=temp[0];
for(int i=0;i<temp.size()-1;i++){
// cout<<temp[i]<<" "<<temp[i+1]<<endl;
ans+=temp[i+1]-temp[i]-1;
}
}
return ans;
}
// Driver Code
int main()
{
int T;
int N;
cin>>T;
while(T--){
cin>>N;
int a[N];
for(int i=0;i<N;i++)
cin>>a[i];
cout <<findSubarrCnt(a, N)<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment