Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
711 B
1
Indexable
Never
class Solution {
public:
    vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {
        vector < int > cumulativeXor(arr.size());
        
        vector < int > ans;
        
        cumulativeXor[0] = arr[0];
        
        for(int i=1;i<arr.size();i++) {
            cumulativeXor[i] = cumulativeXor[i-1] ^ arr[i];
        }
        
        for(int i=0;i<queries.size();i++) {
            int l = queries[i][0], r = queries[i][1];
            
            if(l-1 < 0) {
                ans.push_back(cumulativeXor[r]);
            }
            else {
                ans.push_back(cumulativeXor[r] ^ cumulativeXor[l-1]);
            }
        }
        
        return ans;
    }
};
Leave a Comment