XOR Queries of a Subarray (LeetCode 1310): Solution and Explanation
For each query [l, r], return the XOR of arr[l] through arr[r]. For arr = [1,3,4,8] and queries = [[0,1],[1,2],[0,3],[3,3]] the answer is [2,7,14,8].
Intuition
Answering each query by looping over its range costs O(n) per query. With 3 * 10^4 queries on 3 * 10^4 elements that is about 10^9 operations.
Range sums are solved with prefix sums and subtraction. XOR has the same structure, and its version of subtraction is XOR itself, because x ^ x = 0.
Let prefix[i] be the XOR of the first i elements. prefix[r + 1] covers arr[0..r] and prefix[l] covers arr[0..l-1]. XORing them cancels the shared part and leaves exactly arr[l..r].
Approach: Prefix XOR
- Create
prefixof length n + 1 withprefix[0] = 0. - For each
i, setprefix[i + 1] = prefix[i] ^ arr[i]. - For each query
[l, r], computeprefix[r + 1] ^ prefix[l]and append it to the answer. - Return the answer array.
Step-by-step example
arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
- Build prefix: [0, 1, 1 ^ 3 = 2, 2 ^ 4 = 6, 6 ^ 8 = 14].
- Query [0,1]: prefix[2] ^ prefix[0] = 2 ^ 0 = 2.
- Query [1,2]: prefix[3] ^ prefix[1] = 6 ^ 1 = 7.
- Query [0,3]: prefix[4] ^ prefix[0] = 14. Query [3,3]: prefix[4] ^ prefix[3] = 14 ^ 6 = 8.
- The answer is [2,7,14,8].
Code: Python, Java, C++
1class Solution:
2 def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
3 prefix = [0]
4 for x in arr:
5 prefix.append(prefix[-1] ^ x)
6 ans = []
7 for l, r in queries:
8 ans.append(prefix[r + 1] ^ prefix[l])
9 return ans1class Solution {
2 public int[] xorQueries(int[] arr, int[][] queries) {
3 int n = arr.length;
4 int[] prefix = new int[n + 1];
5 for (int i = 0; i < n; i++) {
6 prefix[i + 1] = prefix[i] ^ arr[i];
7 }
8 int[] ans = new int[queries.length];
9 for (int q = 0; q < queries.length; q++) {
10 ans[q] = prefix[queries[q][1] + 1] ^ prefix[queries[q][0]];
11 }
12 return ans;
13 }
14}1class Solution {
2public:
3 vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {
4 int n = arr.size();
5 vector<int> prefix(n + 1, 0);
6 for (int i = 0; i < n; i++) {
7 prefix[i + 1] = prefix[i] ^ arr[i];
8 }
9 vector<int> ans;
10 for (auto& q : queries) {
11 ans.push_back(prefix[q[1] + 1] ^ prefix[q[0]]);
12 }
13 return ans;
14 }
15};Time and space complexity
O(n + q)
One pass builds the prefix array and each of the q queries takes constant time.
O(n)
The prefix array has n + 1 entries, not counting the output.
Other approaches
Brute force per query (time O(n * q), space O(1))
Loop from l to r for every query and XOR the elements. Simple, but O(n) per query.
In-place prefix (time O(n + q), space O(1))
Overwrite arr[i] with arr[i - 1] ^ arr[i], then answer with arr[r] ^ arr[l - 1] (or arr[r] when l = 0). Saves the extra array but modifies the input.
Common mistakes
- Off-by-one indexing. With an n + 1 prefix array the formula is
prefix[r + 1] ^ prefix[l], notprefix[r] ^ prefix[l]. - Forgetting the l = 0 case when using an in-place prefix of length n. The extra leading 0 in the n + 1 version avoids that special case.
- Thinking XOR needs a separate inverse operation. Unlike sums, XOR undoes itself, so the same operator both builds and splits the prefix.
Frequently asked questions
Why does XOR cancel the prefix before l?
Every element in arr[0..l-1] appears in both prefix[r + 1] and prefix[l]. XOR is commutative and x ^ x = 0, so each of those elements is removed, leaving only arr[l..r].
Can the answer overflow?
No. XOR never produces a bit higher than the highest bit of its inputs, and every value is at most 10^9, which fits in 30 bits.
Is this the same idea as Range Sum Query Immutable?
Yes. That problem uses prefix sums and subtraction. Here the operation is XOR, which is its own inverse.