1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00
LeetCode/go/count-triplets-that-can-form-two-arrays-of-equal-xor.go
Matej Focko d2ef757754
go: allow testing
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-16 11:44:08 +02:00

26 lines
439 B
Go

package main
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
}