1
class Solution:
2
def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
3
"""
4

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

7
find pref xor of arr
8

9
pref = [x,x,x,x]
10

11
for each query find the left and right indices
12
the xor for range (l, r) would be pref[r] xor pref[l-1]
13

14
"""
15
n, m = len(queries), len(arr)
16

17
answer = [1] * n
18

19
pref = [1] * m
20
pref[0] = arr[0]
21
if m > 1:
22
for i in range(1, m):
23
pref[i] = pref[i - 1] ^ arr[i]
24

25
for i, (l, r) in enumerate(queries):
26
if l == 0:
27
answer[i] = pref[r]
28
else:
29
answer[i] = pref[r] ^ pref[l - 1]
30

31
return answer

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0