XOR Queries of a Subarray

You are given an array `arr` of positive integers and an array `queries` where `queries[i] = [left, right]`. For each query, compute the XOR of the elements from index `left` to index `right` inclusive, `arr[left] ^ arr[left + 1] ^ ... ^ arr[right]`. Return an array `answer` where `answer[i]` is the result of the `i`th query.

30:00

XOR Queries of a Subarray
medium
Free written solution
Topics
Companies

You are given an array arr of positive integers and an array queries where queries[i] = [left, right]. For each query, compute the XOR of the elements from index left to index right inclusive, arr[left] ^ arr[left + 1] ^ ... ^ arr[right]. Return an array answer where answer[i] is the result of the ith query.

Example 1:
Input: {"arr":[1,3,4,8],"queries":[[0,1],[1,2],[0,3],[3,3]]}
Output: [2,7,14,8]
Constraints:
  • 1≤arr.length,queries.length≤3×1041 \leq arr.\text{length}, queries.\text{length} \leq 3 \times 10^4

  • 1≤arr[i]≤1091 \leq arr[i] \leq 10^9

  • queries[i].length=2queries[i].\text{length} = 2

  • 0≤lefti≤righti<arr.length0 \leq left_i \leq right_i < arr.\text{length}

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[1,3,4,8],"queries":[[0,1],[1,2],[0,3],[3,3]]}

arr

1
3
4
8

0

1

2

3

prefix (prefix[i] = XOR of arr[0..i-1], sits between cells)

0
?
?
?
?

p0

p1

p2

p3

p4

prefix[0] = 0, the XOR of nothing. prefix[i] will hold the XOR of the first i elements.

answer

[]

Variables
VariableValue
answered0
DepthFunction Call
Stack empty
0/13