mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
26 lines
487 B
Go
26 lines
487 B
Go
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
|
|
}
|