1017. Range XOR Lookup

Medium3.5 points estimatedArrayBit ManipulationPrefix Sum3 hints

A telemetry board has n channels in a row, and channel i reports a status word nums[i], a non-negative integer used as a set of bit flags. To spot faulty hardware, an engineer asks questions queries[j] = [l, r]: combine the status words of channels l through r (0-indexed, both included) with bitwise XOR, so a flag stays on only if it is on in an odd number of those channels.

For every question, return that XOR value. The result is an array in the same order as queries. The status words never change between questions, and windows may overlap or repeat.

Example 1

Input:
nums = [5,1,6,2,7], queries = [[0,2],[1,3],[4,4],[0,4]]
Output:
[2,5,7,7]
Explanation:

5^1^6 = 2; 1^6^2 = 5; the single slot 4 gives 7; all five: 5^1^6^2^7 = 7.

Example 2

Input:
nums = [9,9,9], queries = [[0,1],[0,2],[1,1]]
Output:
[0,9,9]
Explanation:

Equal values cancel in pairs: 9^9 = 0, 9^9^9 = 9, and a single 9 is 9.

Example 3

Input:
nums = [12,10], queries = [[0,1]]
Output:
[6]
Explanation:

12 is 1100 and 10 is 1010 in binary, so their xor is 0110, which is 6.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ queries.length ≤ 105
  • 0 ≤ nums[i] ≤ 109
  • 0 ≤ l ≤ r < nums.length

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms

Expected complexity

Time
O(n + q)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…