Largest Combination With Bitwise AND Greater Than Zero - Problem

The bitwise AND of an array nums is the bitwise AND of all integers in nums.

For example, for nums = [1, 5, 3], the bitwise AND is equal to 1 & 5 & 3 = 1.
Also, for nums = [7], the bitwise AND is 7.

You are given an array of positive integers candidates. Compute the bitwise AND for all possible combinations of elements in the candidates array.

Return the size of the largest combination of candidates with a bitwise AND greater than 0.

Input & Output

Example 1 — Basic Case
$ Input: candidates = [16,17,71,62,12,24,14]
Output: 4
💡 Note: The combination [16,17,24,30] has AND = 16 > 0. We can verify bit 4 is set in exactly 4 numbers, which is the maximum.
Example 2 — Small Array
$ Input: candidates = [8,8]
Output: 2
💡 Note: Both numbers are identical, so we can pick both and their AND = 8 > 0.
Example 3 — No Common Bits
$ Input: candidates = [1,2,4,8]
Output: 1
💡 Note: Each number has a unique bit set, so maximum combination size with AND > 0 is 1.

Constraints

  • 1 ≤ candidates.length ≤ 105
  • 1 ≤ candidates[i] ≤ 107

Visualization

Tap to expand
Largest Combination With Bitwise AND > 0Input: [16,17,71,62,12,24,14]Step 1: Convert to binary representation16=10000, 17=10001, 71=1000111, 62=111110, 12=1100, 24=11000, 14=1110Step 2: Count bit position occurrencesBit 0: 1 count, Bit 1: 2 count, Bit 2: 2 count, Bit 3: 2 count, Bit 4: 4 countStep 3: Maximum count = 4Combination: {16,17,24,30} all have bit 4 setOutput: 4
Understanding the Visualization
1
Input Array
Given array [16,17,71,62,12,24,14] of positive integers
2
Analyze Bits
Convert to binary and count bit position occurrences
3
Find Maximum
Largest count gives us the answer: 4
Key Takeaway
🎯 Key Insight: The largest valid combination will always share the most frequently occurring bit position
Asked in
Meta 25 Google 15
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen