LeetCode 1442: Count Triplets That Can Form Two Arrays of Equal XOR Solution

Master LeetCode problem 1442 (Count Triplets That Can Form Two Arrays of Equal XOR), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

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

Problem Explanation

Explanation

To solve this problem, we can iterate through all possible triplets (i, j, k) and calculate the values of a and b. We then check if a == b for each triplet and count the valid triplets. The key observation here is that a == b is equivalent to arr[i] ^ ... ^ arr[j] == arr[j] ^ ... ^ arr[k].

We can precalculate the XOR prefix sums of the given array to efficiently calculate the XOR of any subarray. Using this, we can iterate through all possible pairs of indices (i, j) and use a hashmap to store the count of XOR values encountered so far. Then, for each valid k, we check if arr[i] ^ ... ^ arr[j] == arr[j] ^ ... ^ arr[k] and increment the count accordingly.

Time Complexity: O(n) where n is the length of the input array arr. Space Complexity: O(n) for the hashmap to store XOR values.

Solution Code

class Solution {
    public int countTriplets(int[] arr) {
        int n = arr.length;
        int[] xorPrefix = new int[n + 1];
        for (int i = 0; i < n; i++) {
            xorPrefix[i + 1] = xorPrefix[i] ^ arr[i];
        }
        
        int count = 0;
        Map<Integer, Integer> xorCount = new HashMap<>();
        
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int a = xorPrefix[i] ^ xorPrefix[j];
                if (xorCount.containsKey(a)) {
                    count += xorCount.get(a);
                }
                if (xorPrefix[j] == xorPrefix[i]) {
                    count++;
                }
                xorCount.put(xorPrefix[j], xorCount.getOrDefault(xorPrefix[j], 0) + 1);
            }
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1442 (Count Triplets That Can Form Two Arrays of Equal XOR)?

This page provides optimized solutions for LeetCode problem 1442 (Count Triplets That Can Form Two Arrays of Equal XOR) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1442 (Count Triplets That Can Form Two Arrays of Equal XOR)?

The time complexity for LeetCode 1442 (Count Triplets That Can Form Two Arrays of Equal XOR) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1442 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1442 in Java, C++, or Python.

Back to LeetCode Solutions