aboutsummaryrefslogtreecommitdiff
path: root/count_triplets_form_arr_xor.py
diff options
context:
space:
mode:
Diffstat (limited to 'count_triplets_form_arr_xor.py')
-rw-r--r--count_triplets_form_arr_xor.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/count_triplets_form_arr_xor.py b/count_triplets_form_arr_xor.py
new file mode 100644
index 0000000..2ba6a59
--- /dev/null
+++ b/count_triplets_form_arr_xor.py
@@ -0,0 +1,32 @@
+# 1442. Count Triplets That Can Form Two Arrays of Equal XOR
+
+"""
+given an array of integers 'arr'. we want to select three indices 'i', 'j',
+and 'k' where '(0 <= i < j <= k < arr.length)'. lets define 'a' and 'b' as
+follows. 'a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]', 'b = arr[j] ^ arr[j +
+1] ^ ... ^ arr[k]'. note that '^' denotes the bitwise xor operation. return
+the number of triplets (i, j, and k) where 'a == b'
+"""
+
+
+class Solution(object):
+ def countTriplets(self, arr):
+ """
+ :type arr: List[int]
+ :rtype: int
+ """
+ arr.insert(0, 0)
+ n, ans = len(arr), 0
+ for i in range(n - 1):
+ arr[i + 1] ^= arr[i]
+ for i in range(n):
+ for j in range(i + 1, n):
+ if arr[i] == arr[j]:
+ ans += j - i - 1
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.countTriplets([2, 3, 1, 6, 7]))
+ print(obj.countTriplets([1, 1, 1, 1, 1]))