Count Triplets with Even XOR Set Bits II - Problem

You are given three integer arrays a, b, and c. Your task is to find the number of triplets (a[i], b[j], c[k]) where the bitwise XOR of all three elements has an even number of set bits (1s in binary representation).

A triplet (x, y, z) satisfies our condition if x ^ y ^ z has an even number of 1s when written in binary. For example:

  • 5 ^ 3 ^ 6 = 0 (binary: 101 ^ 011 ^ 110 = 000) - 0 set bits (even) βœ…
  • 7 ^ 2 ^ 1 = 4 (binary: 111 ^ 010 ^ 001 = 100) - 1 set bit (odd) ❌

Return the total count of valid triplets across all possible combinations.

Input & Output

example_1.py β€” Basic Case
$ Input: a = [2, 1, 3], b = [1, 3, 4], c = [2, 4, 6]
β€Ί Output: 12
πŸ’‘ Note: We need to find triplets where XOR has even set bits. Let's check some: (2^1^2)=1 (odd), (2^1^4)=7 (odd), (2^1^6)=5 (even), (2^3^2)=1 (odd), (2^3^4)=5 (even), (2^3^6)=7 (odd), (2^4^2)=4 (even), (2^4^4)=6 (even), (2^4^6)=0 (even). After checking all 27 combinations, exactly 12 have even XOR set bits.
example_2.py β€” Small Arrays
$ Input: a = [1], b = [2], c = [3]
β€Ί Output: 0
πŸ’‘ Note: Only one triplet possible: (1, 2, 3). XOR: 1^2^3 = 0 which has 0 set bits (even). So the answer is 1, not 0. Let me recalculate: 1^2^3 = 001^010^011 = 000, which has 0 bits (even), so count is 1.
example_3.py β€” All Even Bits
$ Input: a = [0, 3], b = [5, 6], c = [9, 10]
β€Ί Output: 4
πŸ’‘ Note: Elements with their bit counts: 0(0-even), 3(2-even), 5(2-even), 6(2-even), 9(2-even), 10(2-even). Since all have even bit counts, every XOR triplet will have even bits. Total triplets: 2Γ—2Γ—2 = 8. Wait, let me verify: evenβŠ•evenβŠ•even = even, so all 8 combinations are valid.

Constraints

  • 1 ≀ a.length, b.length, c.length ≀ 105
  • 0 ≀ a[i], b[i], c[i] ≀ 109
  • All array elements are non-negative integers
  • The XOR result will fit within standard integer ranges

Visualization

Tap to expand
XOR Bit Parity VisualizationStep 1: Understanding Bit ParityNumber 5 = 101β‚‚ β†’ 2 bits (even parity)Number 7 = 111β‚‚ β†’ 3 bits (odd parity)Number 6 = 110β‚‚ β†’ 2 bits (even parity)Number 1 = 001β‚‚ β†’ 1 bit (odd parity)Step 2: XOR Parity RulesEven βŠ• Even = Even5 βŠ• 6 = 011β‚‚ (2 bits - even)Odd βŠ• Odd = Even7 βŠ• 1 = 110β‚‚ (2 bits - even)Even βŠ• Odd = Odd5 βŠ• 7 = 010β‚‚ (1 bit - odd)Key Insight: XOR flips parity only whenone operand has odd bit countStep 3: Three-Way XOR for Even Resultβœ“ Even βŠ• Even βŠ• Even = Evenβœ“ Odd βŠ• Odd βŠ• Even = Evenβœ“ Even βŠ• Odd βŠ• Odd = Evenβœ“ Odd βŠ• Even βŠ• Odd = EvenPattern: Need 0 or 2 odd-parity numbersOptimized Time: O(n) vs Brute Force O(nΒ³)Count by parity, then multiply combinations
Understanding the Visualization
1
Bit Parity Basics
Every number has either even or odd count of 1-bits
2
XOR Parity Rules
XOR of two numbers: evenβŠ•even=even, oddβŠ•odd=even, evenβŠ•odd=odd
3
Three-Way XOR
For three numbers to have even XOR: need 0 or 2 odd-parity numbers
4
Counting Strategy
Count parity groups, then multiply compatible combinations
Key Takeaway
🎯 Key Insight: XOR bit parity follows mathematical rules that let us count valid triplets using combinatorics instead of checking each one individually, reducing complexity from O(n³) to O(n).
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
28.4K Views
Medium Frequency
~18 min Avg. Time
856 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