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:
parent
af9de3f83b
commit
27d260c9c8
1 changed files with 26 additions and 0 deletions
26
go/count-triplets-that-can-form-two-arrays-of-equal-xor.go
Normal file
26
go/count-triplets-that-can-form-two-arrays-of-equal-xor.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue