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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code