The Largest Combination With Bitwise AND Greater Than Zero interview question asks you to find the maximum number of elements you can pick from an array such that their bitwise AND result is strictly greater than zero. For a bitwise AND of several numbers to be greater than zero, there must be at least one bit position where all chosen numbers have a '1'. If no such bit exists, the result will be 0. The goal is to find the largest subset (combination) that satisfies this condition.
This Array, Bit Manipulation interview pattern is popular at companies like Amazon and Microsoft. It tests a candidate's ability to look past the "combination" aspect of the problem—which usually implies exponential complexity—and recognize the underlying bitwise property. It's a test of observation: instead of trying all combinations, can you see that the problem is really about finding which bit position is shared by the most numbers?
The optimal strategy is to use Counting and Bit Manipulation. Since the numbers are typically within a 32-bit range (often up to , fitting in 24 bits), we can iterate through each bit position from 0 to 31. For each bit position , we count how many numbers in the input array have their -th bit set to 1. The maximum count across all 32 positions will be our answer. This is because any group of numbers that all have the -th bit set will have a bitwise AND result of at least , which is greater than zero.
Consider the array [16, 17, 71, 62, 12, 24, 14].
100001000110001110111110000110000110000001110
Checking bit by bit, the bit with the most '1's wins. If bit 4 is set in 4 numbers, the answer is 4.[1, 2, 4] could work. 1 & 2 & 4 = 0, even though each number is non-zero. They must share a common bit.When dealing with bitwise operators (AND, OR, XOR), always try thinking "bit by bit." Many problems that seem to involve combinations or subsets can be solved by analyzing the behavior of each of the 32 bits independently.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Divide Array Into Equal Pairs | Easy | Solve | |
| Tuple with Same Product | Medium | Solve | |
| Find the Prefix Common Array of Two Arrays | Medium | Solve | |
| Count Pairs That Form a Complete Day II | Medium | Solve | |
| Check If Array Pairs Are Divisible by k | Medium | Solve |