Skip to content

Commit

Permalink
feat(problem-1310/cpp): solve by using a prefix XOR array
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Sep 13, 2024
1 parent e2849cf commit 1fed7d4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion old-problems/1310/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ class Solution {
public:
std::vector<int> xorQueries(
std::vector<int>& arr, std::vector<std::vector<int>>& queries) {
return std::vector(queries.size(), arr.front());
for (std::size_t i{1}; i < arr.size(); ++i) arr[i] ^= arr[i - 1];

std::vector<int> outputs{};
outputs.reserve(queries.size());
for (const auto& query : queries) {
if (query[0] > 0) {
outputs.push_back(arr[query[1]] ^ arr[query[0] - 1]);
} else {
outputs.push_back(arr[query[1]]);
}
}
return outputs;
}
};

0 comments on commit 1fed7d4

Please sign in to comment.