Skip to content
LC-2275 Medium LeetCode

2275. Largest Combination With Bitwise AND Greater Than Zero

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 81% Topics: Array, Hash Table, Bit Manipulation, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogr), r is the max of candidates
# Space: O(logr)

# bit manipulation, freq table
class Solution(object):
    def largestCombination(self, candidates):
        """
        :type candidates: List[int]
        :rtype: int
        """
        cnt = []
        base, mx = 1, max(candidates)
        while base <= mx:
            cnt.append(sum(x&base > 0 for x in candidates))
            base <<= 1
        return max(cnt)

Solution from kamyu104/LeetCode-Solutions · MIT