Skip to content

Commit 7fac59c

Browse files
authored
Create 1310. XOR Queries of a Subarray (#584)
2 parents 6facfc8 + 178b979 commit 7fac59c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

1310. XOR Queries of a Subarray

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {
4+
int n = arr.size();
5+
vector<int> ans;
6+
7+
vector<int> pre(n);
8+
pre[0] = arr[0];
9+
// cout<<pre[0]<<" ";
10+
11+
for(int i=1;i<n;i++){
12+
pre[i] = pre[i-1] ^ arr[i];
13+
// cout<<pre[i]<<" ";
14+
}
15+
// cout<<endl;
16+
17+
for(auto& val:queries){
18+
int l = val[0];
19+
int r = val[1];
20+
21+
if(l==0){
22+
ans.push_back(pre[r]);
23+
continue;
24+
}
25+
26+
int calc = pre[r]^pre[l-1];
27+
ans.push_back(calc);
28+
}
29+
30+
return ans;
31+
}
32+
};

0 commit comments

Comments
 (0)