We need to find triplets (i, j, k) where a = arr[i] XOR arr[i+1] XOR ... XOR arr[j-1] equals b = arr[j] XOR arr[j+1] XOR ... XOR arr[k]. The most direct approach is to try all valid combinations of i, j, and k, compute both XOR values for each triplet, and count when they match.
res to 0.i from 0 to N-2.i, iterate through all possible values of j from i+1 to N-1.j, iterate through all possible values of k from j to N-1.a by XORing elements from index i to j-1.b by XORing elements from index j to k.a == b, increment res.res.class Solution:
def countTriplets(self, arr: List[int]) -> int:
N = len(arr)
res = 0
for i in range(N - 1):
for j in range(i + 1, N):
for k in range(j, N):
a = b = 0
for idx in range(i, j):
a ^= arr[idx]
for idx in range(j, k + 1):
b ^= arr[idx]
if a == b:
res += 1
return resInstead of recomputing the XOR values from scratch for each triplet, we can build them incrementally. As we move j forward, we can extend a by XORing the next element. Similarly, as we move k forward, we can extend b by XORing the next element. This eliminates the innermost loops for computing XOR values.
res to 0.i from 0 to N-2.a to 0.j from i+1 to N-1:a by XORing it with arr[j-1].b to 0.k from j to N-1:b by XORing it with arr[k].a == b, increment res.res.The key insight is that if a == b, then a XOR b == 0, which means arr[i] XOR arr[i+1] XOR ... XOR arr[k] == 0. So we need to find subarrays where the XOR of all elements is 0. For any such subarray from index i to k, we can place j at any position from i+1 to k, giving us k - i valid triplets. This reduces the problem to finding pairs (i, k) where the subarray XOR is 0.
res to 0.i from 0 to N-2.cur_xor to arr[i].k from i+1 to N-1:cur_xor by XORing it with arr[k].cur_xor == 0, add k - i to res (representing all valid positions for j).res.We can use prefix XOR to find subarrays with XOR equal to 0 in linear time. If prefix[i] == prefix[k+1], then the XOR from index i to k is 0. For each prefix value, we track how many times we have seen it and the sum of indices where it occurred. When we see the same prefix again at index k, the contribution to the result is k * count - sum_of_indices, where count is how many times this prefix appeared before, and the formula accounts for all k - i values.
res and prefix to 0.count to store frequency of each prefix value, and index_sum to store sum of indices for each prefix value.count[0] = 1 to handle subarrays starting from index 0.i from 0 to N-1:prefix by XORing it with arr[i].i * count[prefix] - index_sum[prefix] to res.count[prefix] by 1.i + 1 to index_sum[prefix].res.class Solution:
def countTriplets(self, arr: List[int]) -> int:
N = len(arr)
res = prefix = 0
count = defaultdict(int) # number of prefixes
index_sum = defaultdict(int) # sum of indices with that prefix
count[0] = 1
for i in range(N):
prefix ^= arr[i]
if prefix in count:
res += i * count[prefix] - index_sum[prefix]
count[prefix] += 1
index_sum[prefix] += i + 1
return resWhen the XOR from index i to k equals zero, beginners often count it as just 1 triplet. However, j can be placed at any position from i+1 to k, yielding k - i valid triplets for each such pair.
# Incorrect - counts only one triplet per zero-XOR subarray
if cur_xor == 0:
res += 1
# Correct - counts all valid j positions
if cur_xor == 0:
res += k - iIn the optimal solution, the index sum needs to track i + 1 (not i) because we want the sum of starting positions for subarrays, and the formula i * count - index_sum requires this offset. Using just i leads to incorrect results.
# Incorrect - missing the +1 offset
index_sum[prefix] += i
# Correct - accounts for 1-based position in contribution formula
index_sum[prefix] += i + 1The prefix XOR of 0 needs to be pre-initialized to handle subarrays starting from index 0. Without this initialization, valid triplets where the XOR from index 0 to some k equals zero will be missed.
# Incorrect - misses subarrays starting from index 0
count = defaultdict(int)
# Correct - handles prefix XOR that equals a previously seen value at index 0
count = defaultdict(int)
count[0] = 1