Skip to content

Commit f9c336b

Browse files
authored
Update 2354-number-of-excellent-pairs.js
1 parent 3d7932c commit f9c336b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

2354-number-of-excellent-pairs.js

+36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const countExcellentPairs = function(nums, k) {
7+
const n = nums.length, set = new Set()
8+
for(const e of nums) set.add(e)
9+
const cnt = Array(30).fill(0)
10+
for(const e of set) {
11+
const bc = bitCnt(e)
12+
if(cnt[bc] == null) cnt[bc] = 0
13+
cnt[bc] += 1
14+
}
15+
let res = 0
16+
for(let i = 0; i < 30; i++) {
17+
for(let j = 0; j < 30; j++) {
18+
if(i + j >= k) res += cnt[i] * cnt[j]
19+
}
20+
}
21+
22+
return res
23+
24+
function bitCnt(num) {
25+
let res = 0
26+
while(num) {
27+
if(num & 1) res++
28+
num = num >> 1
29+
}
30+
31+
return res
32+
}
33+
};
34+
35+
// another
36+
137
/**
238
* @param {number[]} nums
339
* @param {number} k

0 commit comments

Comments
 (0)