Range Xor Queries

You are given an integer array nums of length n.

You are also given q queries where each query is a pair [a, b].

For every query, compute the bitwise XOR of elements in the subarray from index a to b (1-indexed) (both inclusive).

Return an array answer of length q, where:

  • answer[i] is the XOR result for the ith query.

Important Notes:

  • Queries use 1-based indexing
  • XOR means bitwise exclusive OR

Example 1:

Input: nums = [5, 1, 7, 3, 9], queries = [[1, 3], [2, 5], [4, 4]]

Output: [3, 12, 3]

Explanation:

  • Query [1, 3] → 5 ⊕ 1 ⊕ 7 = 3
  • Query [2, 5] → 1 ⊕ 7 ⊕ 3 ⊕ 9 = 12
  • Query [4, 4] → 3 = 3

Example 2:

Input: nums = [8, 2, 6, 4], queries = [[1, 4], [3, 4]]

Output: [8, 2]

Explanation:

  • Query [1, 4] → 8 ⊕ 2 ⊕ 6 ⊕ 4 = 8
  • Query [3, 4] → 6 ⊕ 4 = 2

Now Your Turn!

Pick the correct output for the given input

Input: nums = [10, 5, 10], queries = [[1, 2]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ n, q ≤ 2 × 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ a ≤ b ≤ n

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<long long> rangeXorQueries(vector<int> &nums, vector<vector<int>> &queries) {
// Your code goes here
}
};
Test Case

Input:

Nums
Queries