Last active
May 15, 2022 03:38
-
-
Save chrisynchen/8b6816cee07c8e6d4a9cff33e8f37b9b to your computer and use it in GitHub Desktop.
6065. Largest Combination With Bitwise AND Greater Than Zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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