> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count Triplets That Can Form Two Arrays of

> Tested Python solution for LeetCode 1442 with 17 pytest cases. Generate a practice environment with lcpy.

LeetCode 1442, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Bit Manipulation](/catalog/topics/bit-manipulation), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/description/).

Generate this problem as a practice environment: tested reference solution, 17 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1442   # by problem number
lcpy gen -s count_triplets_that_can_form_two_arrays_of_equal_xor   # by problem name
```

## Problem

Given an array of integers `arr`.

We want to select three indices `i`, `j` and `k` where `(0 <= i < j <= k < arr.length)`.

Let's 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`.

### Examples

```
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
```

```
Input: arr = [1,1,1,1,1]
Output: 10
```

### Constraints

* `1 <= arr.length <= 300`
* `1 <= arr[i] <= 10^8`

**Follow up:** Can you solve it in `O(n)` time?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_triplets_that_can_form_two_arrays_of_equal_xor/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_triplets_that_can_form_two_arrays_of_equal_xor/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def count_triplets(self, arr: list[int]) -> int:
        # a == b means arr[i..k] xors to 0, i.e. pref[i] == pref[k + 1];
        # each such pair (i, k) contributes k - i triplets (one per j in (i, k]).
        total = 0
        count = {0: 1}
        index_sum = {0: 0}
        prefix = 0
        for m, value in enumerate(arr):
            prefix ^= value
            c = count.get(prefix, 0)
            s = index_sum.get(prefix, 0)
            total += c * m - s
            count[prefix] = c + 1
            index_sum[prefix] = s + m + 1
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.