Skip to content

Commit 6f5b771

Browse files
authored
Create largest-combination-with-bitwise-and-greater-than-zero.cpp
1 parent c8f0a18 commit 6f5b771

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(nlogr), r is the max of candidates
2+
// Space: O(logr)
3+
4+
// bit manipulation, freq table
5+
class Solution {
6+
public:
7+
int largestCombination(vector<int>& candidates) {
8+
vector<int> cnt;
9+
const int mx = *max_element(cbegin(candidates), cend(candidates));
10+
for (int base = 1; base <= mx; base <<= 1) {
11+
cnt.emplace_back();
12+
for (const auto& x : candidates) {
13+
if (x & base) {
14+
++cnt.back();
15+
}
16+
}
17+
}
18+
return *max_element(cbegin(cnt), cend(cnt));
19+
}
20+
};

0 commit comments

Comments
 (0)