XOR Queries of a Subarray

Medium Topics Company Tags

For each inclusive range [left,right] in queries, compute the bitwise XOR of arr[left] through arr[right]. Return the answers in the same order as the queries. Array indices are zero-based, and queries may overlap or repeat.

Example 1:

Input: arr = [5,7,5]
queries = [[0,2],[1,1],[0,1]]

Output: [7,7,2]

Explanation: The full range gives 5 XOR 7 XOR 5 = 7 because equal values cancel. The single-element range gives 7, and 5 XOR 7 = 2 for the last query.

Example 2:

Input: arr = [6,6]
queries = [[0,1],[1,1]]

Output: [0,6]

Explanation: The two equal values cancel to 0. The second query contains only the last value, so its answer is 6.

Example 3:

Input: arr = [9]
queries = [[0,0],[0,0]]

Output: [9,9]

Explanation: Both queries select the only array element. Each answer is 9, and repeated queries keep their positions in the output.

Constraints:

  • 1 <= arr.length, queries.length <= 30000
  • 1 <= arr[i] <= 1000000000
  • Every query contains exactly two indices with 0 <= left <= right < arr.length.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.