Count Triplets with Even XOR Set Bits I - Problem

Given three integer arrays 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.

A set bit is a bit that is equal to 1. The XOR operation combines three numbers: a[i] ^ b[j] ^ c[k]. Count how many bits are 1 in the result - if this count is even, include the triplet in your answer.

Input & Output

Example 1 — Basic Case
$ Input: a = [2,1], b = [3,4], c = [1,2]
Output: 4
💡 Note: All 4 triplets work: (2,3,1)→0 (even), (2,4,2)→0 (even), (1,3,2)→0 (even), (1,4,1)→4 (even)
Example 2 — Single Elements
$ Input: a = [1], b = [2], c = [3]
Output: 0
💡 Note: Only triplet is (1,2,3): 1^2^3=0, which has 0 set bits (even), so result is 1
Example 3 — All Same Parity
$ Input: a = [1,3], b = [1,3], c = [1,3]
Output: 0
💡 Note: All numbers have odd set bits. odd^odd^odd=odd, so no valid triplets

Constraints

  • 1 ≤ a.length, b.length, c.length ≤ 105
  • 0 ≤ a[i], b[i], c[i] ≤ 109

Visualization

Tap to expand
Count Triplets with Even XOR Set BitsArrays:21a34b12cExample Triplet: (2, 3, 1)XOR: 2 ^ 3 ^ 1 = 010 ^ 011 ^ 001 = 000Set bits in 000: 0 (even) ✓Check all 2×2×2 = 8 possible tripletsOutput: Count of valid triplets = 4
Understanding the Visualization
1
Input Arrays
Three arrays with various integers
2
XOR Operation
Calculate a[i] ^ b[j] ^ c[k] for each triplet
3
Count Set Bits
Count 1s in binary representation and check if even
Key Takeaway
🎯 Key Insight: XOR parity can be predicted mathematically without checking every combination
Asked in
Google 15 Microsoft 12 Amazon 8
12.0K Views
Medium Frequency
~15 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen