> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# XOR Queries of a Subarray Python Solution

> Tested Python solution for LeetCode 1310 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 1310, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Bit Manipulation](/catalog/topics/bit-manipulation), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/xor-queries-of-a-subarray/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1310   # by problem number
lcpy gen -s xor_queries_of_a_subarray   # by problem name
```

## Problem

You are given an array arr of positive integers. You are also given the array queries where queries\[i] = \[lefti, righti].

For each query i, compute the XOR of elements from lefti to righti (that is, arr\[lefti] XOR arr\[lefti + 1] XOR ... XOR arr\[righti]).

Return an array answer where answer\[i] is the answer to the ith query.

### Examples

```
Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
Output: [2,7,14,8]
Explanation:
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
```

```
Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
Output: [8,0,4,4]
```

### Constraints

* 1 \<= arr.length, queries.length \<= 3 \* 10^4
* 1 \<= arr\[i] \<= 10^9
* queries\[i].length == 2
* 0 \<= lefti \<= righti \< arr.length

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/xor_queries_of_a_subarray/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/xor_queries_of_a_subarray/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from itertools import accumulate


class Solution:
    def xor_queries(self, arr: list[int], queries: list[list[int]]) -> list[int]:
        prefix = [0, *accumulate(arr, lambda acc, value: acc ^ value)]
        return [prefix[right + 1] ^ prefix[left] for left, right in queries]
```

## Complexity

| Time | Space |
| - | - |
| - | - |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.