Premium Only Content

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. Compute the bitwise AND for all possible combinations of elements in the candidates array.
Return the size of the largest combination of candidates with a bitwise AND greater than 0.
Example 1:
Input: candidates = [16,17,71,62,12,24,14]
Output: 4
Explanation: The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
The size of the combination is 4.
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
Note that more than one combination may have the largest size.
For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.
Example 2:
Input: candidates = [8,8]
Output: 2
Explanation: The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
The size of the combination is 2, so we return 2.
Constraints:
1 <= candidates.length <= 105
1 <= candidates[i] <= 107
class Solution {
public:
int largestCombination(vector<int>& candidates) {
int limit = 24;
int ans=0;
for(int i=0; i<limit; i++){
int count=0;
for(int c : candidates){
if(c>>i & 1){
count++;
ans = max(ans,count);
}
}
}
return ans;
}
};
class Solution {
public:
int largestCombination(vector<int>& candidates) {
int limit = 24;
vector<int> ans;
for(int i=23; i>=0; i--){
int count=0;
vector<int> temp;
for(int c : candidates){
if(c>>i & 1){
temp.push_back(c);
}
}
if(ans.size() < temp.size()){
ans = temp;
}
}
for(int a : ans)
cout<<a<<" ";
return ans.size();
}
};
-
11:04
Nate The Lawyer
3 days ago $3.10 earnedJudge, Mayor & 13 Democrats Arrested In Massive Mail-In Voter Fraud Scandal
7.97K30 -
50:15
daniellesmithab
1 day agoKick-Starting Careers for Young Albertans
8.44K -
39:01
Stephen Gardner
16 hours ago🔥This Will Leave You FURIOUS... What’s Really Going On?
62.8K127 -
19:33
DeVory Darkins
18 hours ago $10.20 earnedBREAKING: Charlie Kirk's shooter in custody after making chilling confession
33.7K147 -
2:12:43
TimcastIRL
11 hours agoErika Kirk Addresses Public After Charlie Kirk Assassination, Live Coverage | Timcast IRL
416K413 -
30:59
The Charlie Kirk Show
11 hours agoCharlie Kirk's beloved wife, Mrs. Erika Kirk addresses the Nation.
567K1.66K -
1:53:28
Man in America
19 hours agoLIVE: Assassin Arrested? Civil War? Are We Being Played?? | LET'S TALK
114K156 -
2:10:33
Badlands Media
16 hours agoOnlyLands Ep. 24: Processing Tragedy, Cancel Culture, and the Next Spark
77.4K30 -
2:27:53
TheSaltyCracker
11 hours agoGot Him ReeEEStream 9-12-25
296K403 -
52:11
Sarah Westall
13 hours agoBread and Circus Keeps you Financially Ignorant – Its Better for the Elites w/ Chris Russo
78.8K8