The Indian Engineer

Problem 2275 Largest Combination With Bitwise AND Greater Than Zero

Posted on 6 mins

Array Hash-Table Bit-Manipulation Counting

Problem Statement

Link - Problem 2275

Question

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

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.

Example 1:

Input: candidates = [16,17,71,62,12,24,14]
Output: 4
Explanation: The combination [16,17,62,24] has a bitwise AND of 16 & 17 & 62 & 24 = 16 > 0.
The size of the combination is 4.
It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.
Note that more than one combination may have the largest size.
For example, the combination [62,12,24,14] has a bitwise AND of 62 & 12 & 24 & 14 = 8 > 0.

Example 2:

Input: candidates = [8,8]
Output: 2
Explanation: The largest combination [8,8] has a bitwise AND of 8 & 8 = 8 > 0.
The size of the combination is 2, so we return 2.

Constraints:

Solution

class Solution {
public:
    int largestCombination(vector<int>& candidates) {
        vector<int>bit_count(24,0);
        for(int i=0;i<candidates.size();i++){
            int temp = candidates[i];
            int j=0;

            while(temp>0&&j<24){
                if((temp&1)==1){
                    bit_count[j]++;
                }
                j++;
                temp= temp>>1;
            }
        }

        int max_count = INT_MIN;
        for(auto it:bit_count)
            max_count = max(max_count,it);

        return max_count;
    }
};

Complexity Analysis

| Algorithm         | Time Complexity | Space Complexity |
| ----------------- | --------------- | ---------------- |
| Bitwise Iteration | O(n log n)      | O(1)             |

Explanation

Intial Thoughts

To start solving this problem, consider the properties of bitwise AND operations, such as the fact that it returns 1 only if both bits are 1. Next, think about how to represent the given numbers in binary form and how their bitwise AND would look. It’s also essential to consider how to generate all possible combinations of elements in the candidates array. Think about the concept of a bit count and how it can help in solving this problem. Consider the time complexity and how to optimize it. Reflect on the fact that we are looking for the largest combination, so we need to find the maximum size of a combination with a bitwise AND greater than 0.

Intuitive Analysis

The solution to this problem involves counting the number of 1s in each bit position across all numbers in the array. To intuitively solve this problem, think of each bit position as a separate ‘slot’ where we count how many numbers have a 1 in that position. Then, look for the slot with the highest count because that’s the bit position where the most numbers have a 1, indicating the largest combination with a bitwise AND greater than 0. Analyze how the while loop and the bitwise operations work together to achieve this. Consider the idea that if a bit position has the maximum count, it means that all numbers in the largest combination have a 1 in that position, resulting in a bitwise AND greater than 0 for that combination.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

For the bitwise AND to be greater than zero, at least one bit should be 1 for every number in the combination.

The candidates are 24 bits long, so for every bit position, we can calculate the size of the largest combination such that the bitwise AND will have a 1 at that bit position.


Similar Questions:

Title URL Difficulty
Count Number of Maximum Bitwise-OR Subsets https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets Medium