1442. Count Triplets That Can Form Two Arrays of Equal XOR

Tapan RachchhTapan Rachchh
1 min read
class Solution:
    def countTriplets(self, arr: List[int]) -> int:    
        ans = 0           
        l = len(arr)
        cache = {}
        for i in range(0, l - 1):
            for j in range(i + 1, l):
                for k in range(j, l):
                    a1 = 0
                    b1 = 0

                    if (i, j) in cache:
                        a1 = cache[(i, j)]
                    else:
                        for e1 in arr[i : j]:
                            a1 ^= e1
                        cache[(i, j)] = a1

                    if (j, k + 1) in cache:
                        b1 = cache[(j, k + 1)]
                    else:
                        for e2 in arr[j : (k + 1)]:
                            b1 ^= e2                                                     
                        cache[(j, k + 1)] = b1

                    if a1 == b1:
                        ans += 1

        return ans
0
Subscribe to my newsletter

Read articles from Tapan Rachchh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tapan Rachchh
Tapan Rachchh