Largest Combination With Bitwise AND Greater Than Zero

Medium Topics Company Tags

Select any nonempty subset of positions from candidates. Its bitwise AND must be greater than zero. Return the greatest number of positions you can select. Positions need not be contiguous, and equal values at different positions may both be selected.

Example 1:

Input: candidates = [3,5,6]

Output: 2

Explanation: Choose 3 and 5: their bitwise AND is 1. Choosing all three values gives AND 0, so the largest valid combination has two entries.

Example 2:

Input: candidates = [7,7,7]

Output: 3

Explanation: The bitwise AND of all three entries is 7, which is positive, so all three can be chosen.

Example 3:

Input: candidates = [1,2,4]

Output: 1

Explanation: The values 1, 2, and 4 have different single set bits. Any pair has AND 0, but any one entry has a positive AND.

Constraints:

  • 1 <= candidates.length <= 100000
  • 1 <= candidates[i] <= 10000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.