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 to1 & 5 & 3 = 1
. - Also, for
nums = [7]
, the bitwise AND is7
.
You are given an array of positive integers candidates
. Evaluate the bitwise AND of every combination of numbers of candidates
. Each number in candidates
may only be used once in each combination.
Return the size of the largest combination of candidates
with a bitwise AND greater than 0
.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Video explanation
Here is the video explaining below methods in detail. Please check it out:
Method 1 - Naive
Here is the naive approach:
- Generate all possible subsets of the list.
- Calculate the bitwise AND for each subset.
- Track the maximum size of subsets where bitwise AND is greater than 0.
Complexity
- ⏰ Time complexity:
O(n * 2^n)
- Generating all subsets takes
O(2^n)
wheren
is the number of elements in the list. - Each subset AND calculation takes
O(n)
.
- Generating all subsets takes
- 🧺 Space complexity:
O(n)
, for storing combinations.
Method 2 - Count max number of bit set at 1 position
To solve this problem, we leverage the bit representation of each number in the array. The intuition is to count the number of 1
s in the bit representation of each element. By doing so, the highest count of 1
s at any given bit position across the entire array gives us the answer. This might seem challenging, but it becomes much clearer when we visualize the bitwise representation and count the 1
s for each bit position.
|
|
Here is the approach:
- Initialize an array of size 32 (to handle up to 32-bit integers) to keep a count of
1
s at each bit position. - For each number in the given array:
- Traverse from the least significant to the most significant bit.
- Update the count in the bits array for each
1
encountered.
- After processing all numbers, find the maximum count from the bits array, which represents the largest combination size where the bitwise AND is greater than 0.
To count the bit set, we can either use modulo (%
) operator or &
operator:
Both methods check the least significant bit (LSB):
- Using
num % 2 == 1
: If the remainder when divided by 2 is 1, the LSB is set. - Using
num & 1 == 1
: If the bitwise AND with 1 is 1, the LSB is set.
Code
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- For each number in the array, it takes up to 32 operations to shift through all bits (since we’re dealing with up to 32-bit integers).
- Since there are
n
numbers, the total time complexity isO(n * 32)
.
- 🧺 Space complexity:
O(1)
- We are using a fixed-size array of length 32 to store the count of set bits for each bit position. Other variables used occupy constant space.
- Thus, the space complexity is
O(1)
.
Method 3 - Count the bits using bitmask
This optimized approach counts the number of 1
s at each bit position in all the elements of the given array. The intuition is to loop through all 32 bit positions and count how many numbers have a 1
in the current bit position. The highest count of 1
s for any bit position across all numbers will be the answer.
Here is the approach:
- Initialize a variable to keep track of the answer.
- Iterate through each bit position from 0 to 31.
- For each bit position, count how many numbers have a
1
in this position using the bitwise AND (&
) operator. - Update the answer with the maximum count found across all bit positions.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(1)