XOR Queries of a Subarray
Answer many questions of the form "what is the XOR of this stretch of the array?", each with one XOR of two stored values.
Given an array arr[left, right]arr[left]arr[left + 1]arr[right]
Example 1
Output: [2, 7, 14, 8]
Example 2
Output: [8, 0, 4, 4]
The Idea
Along a highway, small posts show how many kilometres you have driven from the start. To know how far it is from exit 2 to exit 5, nobody measures that stretch again. You read the two posts and subtract. This problem asks the same kind of question about a stretch of an array, with XOR in place of distance.
What does XOR do to two numbers? Write them in binary. In Example 1, 3 is 001101000111^3 ^ 4
Now walk through Example 1. The query [1, 2]3 ^ 4[0, 3]1 ^ 3 ^ 4 ^ 8[0, 1][1, 2][0, 3]
It can, because XOR undoes itself. In 1 ^ 11 ^ 3 ^ 4 ^ 13 ^ 4
So keep a post at every point of the array: the XOR of all numbers before that point. This is a running XOR, kept in an array named prefixprefix[i]iprefix
The row labelled running XOR starts with prefix[0] = 01 ^ 3 = 22 ^ 4 = 66 ^ 8 = 14[1, 2]arr
In the bit table at the bottom, prefix[3] = 61 ^ 3 ^ 4prefix[1]arr[0]3 ^ 40111
Solution Approach
Looping over each stretch works, but it repeats work. The running XOR instead needs three decisions: storing the running XOR, picking the two entries for a query, and checking that the answer cannot overflow.
Looping over each stretch
What does the simple way cost? For each query, start from 0 and XOR in arr[left]arr[right]
Storing the running XOR
How long should prefixarrprefix[i + 1] = prefix[i] ^ arr[i]prefix[0]
Picking the two entries
Which two entries answer [left, right]prefix[right + 1]arr[0]arr[right]prefix[left]arr[0]arr[left - 1]arr[left]arr[right][1, 2]prefix[3]prefix[1]
Why the answer fits in an int
Could the answer grow too big, as a sum of many numbers can? A Java intarrint
Java Solution
The method xorQueries
- First, create the array
withprefix slots. Java fills a newarr.length + 1 array with zeros, soint is already 0.prefix[0] - A loop with index
walks alongi and setsarr , one more number each time.prefix[i + 1] = prefix[i] ^ arr[i] - Create the array
, with one slot per query. A second loop, with indexanswer , reads each query'sq andleft .right - Set
toanswer[q] . When the second loop ends, the method returnsprefix[right + 1] ^ prefix[left] .answer
The mainInteger.MAX_VALUE
import java.util.Arrays;
public class XorQueries {
public static int[] xorQueries(int[] arr, int[][] queries) {
// prefix[i] = arr[0] ^ arr[1] ^ ... ^ arr[i - 1], and prefix[0] = 0
int[] prefix = new int[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
prefix[i + 1] = prefix[i] ^ arr[i];
}
int[] answer = new int[queries.length];
for (int q = 0; q < queries.length; q++) {
int left = queries[q][0];
int right = queries[q][1];
// prefix[left] is in both, so XOR-ing it again cancels it
answer[q] = prefix[right + 1] ^ prefix[left];
}
return answer;
}
public static void main(String[] args) {
int[][] queries1 = {{0, 1}, {1, 2}, {0, 3}, {3, 3}};
System.out.println(Arrays.toString(xorQueries(new int[] {1, 3, 4, 8}, queries1)));
int[][] queries2 = {{2, 3}, {1, 3}, {0, 0}, {0, 3}};
System.out.println(Arrays.toString(xorQueries(new int[] {4, 8, 2, 10}, queries2)));
int[][] queries3 = {{0, 1}, {0, 2}, {1, 1}};
System.out.println(Arrays.toString(xorQueries(new int[] {7, 7, 7}, queries3)));
int[] big = {1000000000, 999999999, 2147483647};
int[][] queries4 = {{0, 1}, {0, 2}};
System.out.println(Arrays.toString(xorQueries(big, queries4)));
System.out.println(Arrays.toString(xorQueries(new int[] {5}, new int[][] {{0, 0}})));
}
}
Running it prints:
[2, 7, 14, 8]
[8, 0, 4, 4]
[0, 7, 7]
[1023, 2147482624]
[5]
The lines [2, 7, 14, 8][8, 0, 4, 4][1, 3]8 ^ 2
The line [0, 7, 7][7, 7, 7][1, 1]
Look at [1023, 2147482624]Integer.MAX_VALUEintint[5]
The + 1prefix[right + 1]prefix[right] ^ prefix[left][1, 3, 6, 0][2, 7, 14, 8]arr[right][3, 3]
Complexity
Building the running XOR takes one pass over arr
- Time: O(n + q), because the first loop visits each of the n numbers once, and each of the q queries reads two entries of
and XORs them.prefix - Space: O(n) besides the output, because
keeps n + 1 running values, whileprefix , with its q entries, is the result itself.answer
With addition in place of XOR, the same idea answers sums over a stretch, where the shared part is subtracted: see