|
1 | 1 | # [Problem 1310: XOR Queries of a Subarray](https://leetcode.com/problems/xor-queries-of-a-subarray/description/?envType=daily-question)
|
2 | 2 |
|
3 | 3 | ## Initial thoughts (stream-of-consciousness)
|
| 4 | +- There might be an "efficient" way to do this, but I'm just going to compute this manually |
| 5 | +- For each query (`[left, right]`), we can loop through from `left` to `right`, taking the XOR of the result plus the next element each time |
| 6 | +- Once we reach `right`, we can append the result to the `answer` list |
4 | 7 |
|
5 | 8 | ## Refining the problem, round 2 thoughts
|
| 9 | +- I think this is straightforward...the potential catch is that we might run out of time |
| 10 | +- Maybe there's a way to re-use computations...but let's try the easy version first |
6 | 11 |
|
7 | 12 | ## Attempted solution(s)
|
8 | 13 | ```python
|
9 |
| -class Solution: # paste your code here! |
10 |
| - ... |
| 14 | +class Solution: |
| 15 | + def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: |
| 16 | + answers = [] |
| 17 | + for left, right in queries: |
| 18 | + x = arr[left] |
| 19 | + for i in range(left + 1, right + 1): |
| 20 | + x ^= arr[i] |
| 21 | + answers.append(x) |
| 22 | + return answers |
11 | 23 | ```
|
| 24 | +- Given test cases pass |
| 25 | +- Let's try submitting |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +Hrmph, time limit exceeded. Bummer. |
| 30 | + |
| 31 | +## More brainstorming |
| 32 | +- Sadly I think we'll need to do something more efficient that enables us to re-use computations |
| 33 | +- Let's see...the way to do this is usually to think about fundamental properties of the main operation/function, and see if anything can be exploited. What do we know about XORs? |
| 34 | + - We know XOR is associative-- e.g., A XOR (B XOR C) is the same as (A XOR B) XOR C |
| 35 | + - We know XOR of A with itself is 0 |
| 36 | + - We know XOR of A with 0 is A |
| 37 | + - So... |
| 38 | +- Maybe we can compute the "cumulative XOR" up to each index, `i` |
| 39 | +- Then the query answer would just be XOR(right) XOR XOR(left - 1) -- i.e., the XOR of everything up to `right`, but then XORing out everything before `left` |
| 40 | +- We can compute the initial cumulative pass through in $O(n)$ time (where $n$ is the length of `arr`) |
| 41 | +- The time to compute every query is constant (just a single XOR operation), so the total time is $O(n + q)$ where $n$ is the length of the array and $q$ is the number of queries. But really this is just $O(max(n, q))$. |
| 42 | +- Anyways...let's try it! |
| 43 | +```python |
| 44 | +class Solution: |
| 45 | + def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: |
| 46 | + cumulative_xor = [0] * (len(arr) + 1) |
| 47 | + for i in range(1, len(arr) + 1): |
| 48 | + cumulative_xor[i] = cumulative_xor[i - 1] ^ arr[i - 1] |
| 49 | + |
| 50 | + answers = [] |
| 51 | + for left, right in queries: |
| 52 | + answers.append(cumulative_xor[right + 1] ^ cumulative_xor[left]) |
| 53 | + |
| 54 | + return answers |
| 55 | +``` |
| 56 | +- Given test cases (still) pass |
| 57 | +- Submitting... |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +Solved! |
0 commit comments