Bitwise AND is like if under the binary expression, the number of that digit is the same then it will have an output 1, otherwise 0.
We need to find the combination that the Bitwise AND of all these numbers larger than 1. In other words, we need to find a single digit in binary expression that has the largest occurrences.
So, we will use a dp array to store the occurrences of different digit that is 1 for each numbers.
class Solution {
public:
int largestCombination(vector<int>& candidates) {
int n = candidates.size();
int dp[40] = {0};
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j <= 24; j++){
if(candidates[i] & (1 << j))dp[j]++;
ans = max(ans, dp[j]);
}
}
return ans;
}
};