go: add «1310. XOR Queries of a Subarray»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-13 10:26:03 +02:00
parent d3d901d5e7
commit fe390db878
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,23 @@
package main
func xorQueries(arr []int, queries [][]int) []int {
result := make([]int, len(queries))
// Prefix XOR
for i := 1; i < len(arr); i++ {
arr[i] ^= arr[i-1]
}
// Evaluate queries
for i, query := range queries {
l, r := query[0], query[1]
if l > 0 {
result[i] = arr[l-1] ^ arr[r]
} else {
result[i] = arr[r]
}
}
return result
}