[LeetCode] Count Triplets with Even XOR Set Bits II

3215. Count Triplets with Even XOR Set Bits II

Given three integer arrays a, b, and c, return the number of triplets (a[i], b[j], c[k]), such that the bitwise XOR between the elements of each triplet has an even number of set bits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
long long tripletCount(vector<int>& a, vector<int>& b, vector<int>& c) {
vector<vector<long long>> dp(3,vector<long long>(2));
for(auto& n : a) dp[0][__builtin_popcount(n) & 1]++;
for(auto& n : b) dp[1][__builtin_popcount(n) & 1]++;
for(auto& n : c) dp[2][__builtin_popcount(n) & 1]++;
long long res = 0;
for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) for(int k = 0; k < 2; k++) {
if((i + j + k) & 1) continue;
res += dp[0][i] * dp[1][j] * dp[2][k];
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/23/PS/LeetCode/count-triplets-with-even-xor-set-bits-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.