go: add «1442. Count Triplets That Can Form Two Arrays of Equal XOR»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-05-30 17:57:56 +02:00
parent af9de3f83b
commit 27d260c9c8
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,26 @@
package count_triplets_that_can_form_two_arrays_of_equal_xor
func countTriplets(arr []int) int {
precomputePrefix := func() []int {
prefix := append([]int{0}, arr...)
for i, _ := range arr {
prefix[i+1] ^= prefix[i]
}
return prefix
}
prefix := precomputePrefix()
triplets := 0
for left, _ := range prefix {
for right := left + 1; right < len(prefix); right++ {
if prefix[left] == prefix[right] {
triplets += right - left - 1
}
}
}
return triplets
}