Skip to main content

1310.js

/** ------------------------------------------------------------------------------
*
* 1310. XOR Queries of a Subarray
* Topics: Bit Manipulation
* https://leetcode.com/problems/xor-queries-of-a-subarray/description/
*
------------------------------------------------------------------------------ */
/**
* @param {number[]} arr
* @param {number[][]} queries
* @return {number[]}
*/
var xorQueries = function (arr, queries) {
const len = arr.length
const prefixXOR = Array.from({ length: len })
prefixXOR[0] = arr[0]

for (let i = 1; i < len; i++) {
prefixXOR[i] = prefixXOR[i - 1] ^ arr[i]
}

const answer = []

for (const [m, n] of queries) {
if (m === 0) {
answer.push(prefixXOR[n])
} else {
answer.push(prefixXOR[n] ^ prefixXOR[m - 1])
}
}
return answer
}