Watch 10 video solutions for Count Triplets with Even XOR Set Bits I, a easy level problem involving Array, Bit Manipulation. This walkthrough by NeetCode has 159,557 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
a, b, and c, return the number of triplets (a[i], b[j], c[k]), such that the bitwise XOR of the elements of each triplet has an even number of set bits.
Example 1:
Input: a = [1], b = [2], c = [3]
Output: 1
Explanation:
The only triplet is (a[0], b[0], c[0]) and their XOR is: 1 XOR 2 XOR 3 = 002.
Example 2:
Input: a = [1,1], b = [2,3], c = [1,5]
Output: 4
Explanation:
Consider these four triplets:
(a[0], b[1], c[0]): 1 XOR 3 XOR 1 = 0112(a[1], b[1], c[0]): 1 XOR 3 XOR 1 = 0112(a[0], b[0], c[1]): 1 XOR 2 XOR 5 = 1102(a[1], b[0], c[1]): 1 XOR 2 XOR 5 = 1102
Constraints:
1 <= a.length, b.length, c.length <= 1000 <= a[i], b[i], c[i] <= 100Problem Overview: You are given three arrays and need to count triplets (a, b, c) where one element is chosen from each array. A triplet is valid if the XOR value a ^ b ^ c contains an even number of set bits. The key challenge is recognizing that only the parity of the set bit count matters.
Approach 1: Brute Force Triplets (O(n * m * k) time, O(1) space)
Check every possible triplet by iterating through the three arrays using three nested loops. For each combination, compute x = a ^ b ^ c and count its set bits using a popcount operation. If the number of set bits is even, increment the answer. This approach is straightforward and helps verify correctness, but it becomes slow when arrays grow because the complexity multiplies across all three lengths.
Approach 2: Bit Parity Counting (O(n + m + k) time, O(1) space)
The optimal observation: the parity of set bits in an XOR equals the XOR of the individual parities. In other words, parity(popcount(a ^ b ^ c)) = pa ^ pb ^ pc, where pa, pb, and pc represent whether each number has an odd (1) or even (0) number of set bits. The result is even when this XOR equals 0.
First compute the set-bit parity of every number in each array. Count how many values have even parity and how many have odd parity. Then combine counts instead of enumerating triplets. Valid triplets occur when the XOR of the three parities equals 0, which happens for combinations like (even, even, even) or any case where exactly two are odd. Multiply the corresponding counts to get the total. This converts a triple nested iteration into simple counting.
This problem relies heavily on understanding XOR properties and bit-count parity. If XOR and popcount operations feel unfamiliar, reviewing bit manipulation fundamentals and parity tricks will make these patterns much easier to spot. Similar counting optimizations also appear in problems involving arrays and XOR-based constraints.
Recommended for interviews: Start by explaining the brute force enumeration to show the direct interpretation of the problem. Then introduce the parity insight that collapses the search space. Interviewers typically expect the parity-counting approach because it demonstrates strong understanding of XOR properties and efficient counting techniques.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Triplets | O(n * m * k) | O(1) | Good for understanding the problem or when arrays are extremely small |
| Bit Parity Counting | O(n + m + k) | O(1) | Optimal solution using XOR parity observation; works for large inputs |