From 27d260c9c89e93129c362b2c3c699eb2b6df0629 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 30 May 2024 17:57:56 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1442.=20Count=20Triplets=20Th?= =?UTF-8?q?at=20Can=20Form=20Two=20Arrays=20of=20Equal=20XOR=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...s-that-can-form-two-arrays-of-equal-xor.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 go/count-triplets-that-can-form-two-arrays-of-equal-xor.go diff --git a/go/count-triplets-that-can-form-two-arrays-of-equal-xor.go b/go/count-triplets-that-can-form-two-arrays-of-equal-xor.go new file mode 100644 index 0000000..4d2b8f3 --- /dev/null +++ b/go/count-triplets-that-can-form-two-arrays-of-equal-xor.go @@ -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 +}