[LeetCode] Largest Combination With Bitwise AND Greater Than Zero

2275. Largest Combination With Bitwise AND Greater Than Zero

The bitwise AND of an array nums is the bitwise AND of all integers in nums.

  • For example, for nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.
  • Also, for nums = [7], the bitwise AND is 7.

You are given an array of positive integers candidates. Evaluate the bitwise AND of every combination of numbers of candidates. Each number in candidates may only be used once in each combination.

Return the size of the largest combination of candidates with a bitwise AND greater than 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int largestCombination(vector<int>& candidates) {
vector<int> count(32, 0);
for(auto& c : candidates) {
for(int i = 0; i < 32; i++) {
if(c & (1<<i))
count[i]++;
}
}
return *max_element(begin(count), end(count));
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/15/PS/LeetCode/largest-combination-with-bitwise-and-greater-than-zero/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.