mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
27 lines
487 B
Go
27 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
|
||
|
}
|