-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3215-count-triplets-with-even-xor-set-bits-ii.js
More file actions
47 lines (42 loc) · 1.1 KB
/
3215-count-triplets-with-even-xor-set-bits-ii.js
File metadata and controls
47 lines (42 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 3215. Count Triplets with Even XOR Set Bits II
* https://leetcode.com/problems/count-triplets-with-even-xor-set-bits-ii/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} a
* @param {number[]} b
* @param {number[]} c
* @return {number}
*/
var tripletCount = function(a, b, c) {
const countA = [0, 0];
const countB = [0, 0];
const countC = [0, 0];
for (const num of a) {
countA[countSetBits(num) % 2]++;
}
for (const num of b) {
countB[countSetBits(num) % 2]++;
}
for (const num of c) {
countC[countSetBits(num) % 2]++;
}
let result = 0;
result += countA[0] * countB[0] * countC[0];
result += countA[1] * countB[1] * countC[0];
result += countA[1] * countB[0] * countC[1];
result += countA[0] * countB[1] * countC[1];
return result;
function countSetBits(num) {
let count = 0;
while (num > 0) {
count += num & 1;
num >>= 1;
}
return count;
}
};