[LeetCode] Count Triplets with Even XOR Set Bits I

3199. Count Triplets with Even XOR Set Bits I

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.

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