Skip to content

Instantly share code, notes, and snippets.

@chrisynchen
Last active May 15, 2022 03:38
Show Gist options
  • Save chrisynchen/8b6816cee07c8e6d4a9cff33e8f37b9b to your computer and use it in GitHub Desktop.
Save chrisynchen/8b6816cee07c8e6d4a9cff33e8f37b9b to your computer and use it in GitHub Desktop.
6065. Largest Combination With Bitwise AND Greater Than Zero
public int largestCombination(int[] candidates) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < candidates.length; i++) {
min = Math.min(min, candidates[i]);
max = Math.max(max, candidates[i]);
}
while((min & min - 1) > 0) {
min &= min - 1;
}
int result = 0;
long tempMin = (long)min;
while(tempMin <= max) {
int count = 0;
for(int e: candidates) {
if((e & tempMin) > 0) {
count++;
}
}
tempMin <<= 1;
result = Math.max(result, count);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment