-
Notifications
You must be signed in to change notification settings - Fork 3
/
2354.cpp
26 lines (23 loc) · 803 Bytes
/
2354.cpp
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
class Solution {
public:
long long countExcellentPairs(vector<int>& nums, int k) {
unordered_set<int> numberHash;
vector<int> bitcounts;
for (auto& num : nums) {
if (numberHash.find(num) != numberHash.end()) continue;
int bits = __builtin_popcount(num);
bitcounts.push_back(bits);
numberHash.insert(num);
}
sort(bitcounts.begin(), bitcounts.end());
int n = bitcounts.size();
long long res = 0;
for (int i = 0; i < n; ++i) {
int target = k - bitcounts[i];
auto it = lower_bound(bitcounts.begin(), bitcounts.end(), target);
long long plus = n - (it - bitcounts.begin());
res += plus;
}
return res;
}
};