Skip to main content
Back to problems
Leetcode
Medium
Arrays
Bit Manipulation
Xor Queries Of A Subarray

Answer multiple range XOR queries on an array efficiently.

Acceptance 100%
Problem Statement

You are given an integer array and several queries. Each query asks for the XOR of all elements in a subarray from index ll to index rr inclusive.

For every query, return the XOR value for that range. Since there may be many queries, the solution should be faster than recomputing the XOR from scratch each time.

Input Format

  • An integer array arr of length n.
  • A list of queries, where each query contains two integers l and r.
  • Each query is interpreted as the inclusive subarray arr[l..r].

For this record, treat the problem as returning one answer per query in the same order.

Output Format

  • Return an array of integers where the i-th value is the XOR of arr[l_i..r_i].

If the problem is presented in a function-based format, return the list of answers.

Constraints

  • 1 <= n, q are typically up to large values, so an efficient preprocessing approach is expected.
  • Array values are integers that fit within standard 32-bit integer XOR behavior.
  • Query indices are valid and inclusive.
Examples
Sample cases returned by the problem API.

Example 1

Input

arr = [1, 3, 4, 8]
queries = [[0, 1], [1, 2], [0, 3], [3, 3]]

Output

[2, 7, 14, 8]

Explanation

  • arr[0..1] = 1 XOR 3 = 2
  • arr[1..2] = 3 XOR 4 = 7
  • arr[0..3] = 1 XOR 3 XOR 4 XOR 8 = 14
  • arr[3..3] = 8

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.