548. Count Triplets with Even XOR Set Bits II

Given three integer arrays a, b, and c, find the number of valid triplets (a[i], b[j], c[k]), where: (0 ≤ i < a.length, 0 ≤ j < b.length, 0 ≤ k < c.length).

A triplet is considered valid if the bitwise XOR of its elements has an even number of set bits (bits with value 1).

Return the total count of such triplets.

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 ⊕ 2 ⊕ 3 = 000 (binary, even number of set bits).

Thus, the answer is 1.

Example 2:

Input: a = [1, 1], b = [2, 3], c = [1, 5]

Output: 4

Explanation:

The valid triplets are:

(a[0], b[1], c[0]) → 1 ⊕ 3 ⊕ 1 = 011 (2 set bits, even)

(a[1], b[1], c[0]) → 1 ⊕ 3 ⊕ 1 = 011 (2 set bits, even)

(a[0], b[0], c[1]) → 1 ⊕ 2 ⊕ 5 = 110 (2 set bits, even)

(a[1], b[0], c[1]) → 1 ⊕ 2 ⊕ 5 = 110 (2 set bits, even)

Thus, the total count is 4.

Now Your Turn!

Pick the correct output for the given input

Input: a = [2, 4], b = [3, 1, 6], c = [5, 7]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
long long tripletCount(vector<int>& a, vector<int>& b, vector<int>& c){
}
};
 
 
Test Case

Input:

A
B
C