Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
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)
Example 2:
Input: arr = [1,1,1,1,1] Output: 10
Constraints:
1 <= arr.length <= 3001 <= arr[i] <= 108Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math · Bit Manipulation
[2,3,1,6,7]
[1,1,1,1,1]
find-the-original-array-of-prefix-xor)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1442: Count Triplets That Can Form Two Arrays of Equal XOR
class Solution {
public int countTriplets(int[] arr) {
int ans = 0, n = arr.length;
for (int i = 0; i < n; ++i) {
int s = arr[i];
for (int k = i + 1; k < n; ++k) {
s ^= arr[k];
if (s == 0) {
ans += k - i;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #1442: Count Triplets That Can Form Two Arrays of Equal XOR
func countTriplets(arr []int) (ans int) {
for i, x := range arr {
s := x
for k := i + 1; k < len(arr); k++ {
s ^= arr[k]
if s == 0 {
ans += k - i
}
}
}
return
}
# Accepted solution for LeetCode #1442: Count Triplets That Can Form Two Arrays of Equal XOR
class Solution:
def countTriplets(self, arr: List[int]) -> int:
ans, n = 0, len(arr)
for i, x in enumerate(arr):
s = x
for k in range(i + 1, n):
s ^= arr[k]
if s == 0:
ans += k - i
return ans
// Accepted solution for LeetCode #1442: Count Triplets That Can Form Two Arrays of Equal XOR
impl Solution {
pub fn count_triplets(arr: Vec<i32>) -> i32 {
let mut ans = 0;
let n = arr.len();
for i in 0..n {
let mut s = arr[i];
for k in (i + 1)..n {
s ^= arr[k];
if s == 0 {
ans += (k - i) as i32;
}
}
}
ans
}
}
// Accepted solution for LeetCode #1442: Count Triplets That Can Form Two Arrays of Equal XOR
function countTriplets(arr: number[]): number {
const n = arr.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
let s = arr[i];
for (let k = i + 1; k < n; ++k) {
s ^= arr[k];
if (s === 0) {
ans += k - i;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.