Skip to content

Commit

Permalink
Create 1310. XOR Queries of a Subarray
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Sep 13, 2024
1 parent 6facfc8 commit 178b979
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 1310. XOR Queries of a Subarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {
int n = arr.size();
vector<int> ans;

vector<int> pre(n);
pre[0] = arr[0];
// cout<<pre[0]<<" ";

for(int i=1;i<n;i++){
pre[i] = pre[i-1] ^ arr[i];
// cout<<pre[i]<<" ";
}
// cout<<endl;

for(auto& val:queries){
int l = val[0];
int r = val[1];

if(l==0){
ans.push_back(pre[r]);
continue;
}

int calc = pre[r]^pre[l-1];
ans.push_back(calc);
}

return ans;
}
};

0 comments on commit 178b979

Please sign in to comment.