Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1310.html

1310. XOR Queries of a Subarray (Medium)

Given the array arr of positive integers and the array queries where queries[i] = [Li, Ri], for each query i compute the XOR of elements from Li to Ri (that is, arr[Li] xor arr[Li+1] xor ... xor arr[Ri] ). Return an array containing the result for the given queries.

 

Example 1:

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

Example 2:

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

 

Constraints:

  • 1 <= arr.length <= 3 * 10^4
  • 1 <= arr[i] <= 10^9
  • 1 <= queries.length <= 3 * 10^4
  • queries[i].length == 2
  • 0 <= queries[i][0] <= queries[i][1] < arr.length

Related Topics:
Bit Manipulation

Solution 1.

  • class Solution {
        public int[] xorQueries(int[] arr, int[][] queries) {
            int length = arr.length;
            int[] xors = new int[length];
            xors[0] = arr[0];
            for (int i = 1; i < length; i++)
                xors[i] = xors[i - 1] ^ arr[i];
            int queriesCount = queries.length;
            int[] result = new int[queriesCount];
            for (int i = 0; i < queriesCount; i++) {
                int[] query = queries[i];
                int begin = query[0], end = query[1];
                if (begin == end)
                    result[i] = arr[begin];
                else {
                    if (begin == 0)
                        result[i] = xors[end];
                    else
                        result[i] = xors[end] ^ xors[begin - 1];
                }
            }
            return result;
        }
    }
    
    ############
    
    class Solution {
        public int[] xorQueries(int[] arr, int[][] queries) {
            int n = arr.length;
            int[] xors = new int[n + 1];
            for (int i = 0; i < n; ++i) {
                xors[i + 1] = xors[i] ^ arr[i];
            }
            int m = queries.length;
            int[] ans = new int[m];
            for (int i = 0; i < m; ++i) {
                int l = queries[i][0];
                int r = queries[i][1];
                ans[i] = xors[l] ^ xors[r + 1];
            }
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/xor-queries-of-a-subarray/
    // Time: O(N + Q)
    // Space: O(1)
    class Solution {
    public:
        vector<int> xorQueries(vector<int>& A, vector<vector<int>>& Q) {
            for (int i = 1; i < A.size(); ++i) A[i] ^= A[i - 1];
            vector<int> ans;
            for (auto &q : Q) {
                int from = q[0], to = q[1];
                ans.push_back(A[to] ^ (from ? A[from - 1] : 0));
            }
            return ans;
        }
    };
    
  • class Solution:
        def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
            xors = [0]
            for v in arr:
                xors.append(xors[-1] ^ v)
            return [xors[l] ^ xors[r + 1] for l, r in queries]
    
    ############
    
    # 1310. XOR Queries of a Subarray
    # https://leetcode.com/problems/xor-queries-of-a-subarray/
    
    class Solution:
        def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
            res = []
            prefix = [0]
            for x in arr:
                prefix.append(x ^ prefix[-1])
            
            for l,r in queries:
                res.append(prefix[l] ^ prefix[r+1])
            
            return res
    
  • func xorQueries(arr []int, queries [][]int) []int {
    	xors := make([]int, len(arr)+1)
    	for i, v := range arr {
    		xors[i+1] = xors[i] ^ v
    	}
    	var ans []int
    	for _, q := range queries {
    		l, r := q[0], q[1]
    		ans = append(ans, xors[l]^xors[r+1])
    	}
    	return ans
    }
    
  • /**
     * @param {number[]} arr
     * @param {number[][]} queries
     * @return {number[]}
     */
    var xorQueries = function (arr, queries) {
        let n = arr.length;
        let xors = new Array(n + 1).fill(0);
        for (let i = 0; i < n; i++) {
            xors[i + 1] = xors[i] ^ arr[i];
        }
        let res = [];
        for (let [l, r] of queries) {
            res.push(xors[l] ^ xors[r + 1]);
        }
        return res;
    };
    
    
  • function xorQueries(arr: number[], queries: number[][]): number[] {
        const n = arr.length;
        const s: number[] = new Array(n + 1).fill(0);
        for (let i = 0; i < n; ++i) {
            s[i + 1] = s[i] ^ arr[i];
        }
        const ans: number[] = [];
        for (const [l, r] of queries) {
            ans.push(s[r + 1] ^ s[l]);
        }
        return ans;
    }
    
    

All Problems

All Solutions