2275. Largest Combination With Bitwise AND Greater Than Zero
Tapan Rachchh
1 min read
Way 1 - Using bit matrix
class Solution:
def largestCombination(self, candidates: List[int]) -> int:
maxBits = max(candidates).bit_length()
binaryRepresentations = [[int(x) for x in format(number, f'0{maxBits}b')] for number in candidates]
maxLen = 0
for col in range(len(binaryRepresentations[0])):
temp = 0
for row in range(len(binaryRepresentations)):
temp += binaryRepresentations[row][col]
if temp > maxLen:
maxLen = temp
return maxLen
Way 2 - Using nth bit sum
class Solution:
def largestCombination(self, candidates: List[int]) -> int:
maxLen = 0
for n in range(max(candidates).bit_length()):
temp = 0
for number in candidates:
nthBit = (number >> n) & 1
temp += nthBit
maxLen = max(temp, maxLen)
return maxLen
0
Subscribe to my newsletter
Read articles from Tapan Rachchh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by