go: add «1310. XOR Queries of a Subarray»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d3d901d5e7
commit
fe390db878
1 changed files with 23 additions and 0 deletions
23
go/xor-queries-of-a-subarray.go
Normal file
23
go/xor-queries-of-a-subarray.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue