This repository contains solutions to solve the problem "Largest Combination With Bitwise AND Greater Than Zero". The goal is to find the size of the largest subset from an array of integers such that the bitwise AND of the subset is greater than zero. Below is a step-by-step breakdown of the solution logic used in each language.
- Intuition: The problem requires us to identify the largest group of numbers where, if bitwise AND is applied across all elements in the subset, the result remains greater than zero.
- Bit Manipulation Insight: If a certain bit position has many numbers with a
1
at that position, we can create a large subset with a non-zero bitwise AND. - Optimization Strategy: Instead of calculating bitwise ANDs for each subset, we count how many numbers have a
1
in each bit position (0 to 30), then select the maximum count among these.
- Initialize Count Array: Create an array
bitCount
to hold counts of1
s for each of the 31 bit positions (since integers up to (10^7) have up to 31 bits). - Counting Set Bits:
- For each number in the input array:
- Loop through each bit position from 0 to 30.
- Check if the bit at that position is
1
using bitwise AND (num & (1 << i)
). - If
1
, increment the count inbitCount
for that bit position.
- For each number in the input array:
- Get Maximum Count:
- After counting all bit positions, find the highest value in
bitCount
. - This value represents the maximum subset size where bitwise AND will be greater than zero.
- After counting all bit positions, find the highest value in
- Return Result: The highest count is returned as the solution.
- Initialize Count Array: Create an array
bitCount
to store the count of1
s for each bit position from 0 to 30. - Counting Set Bits:
- For each integer in
candidates
:- Loop through bit positions from 0 to 30.
- Use
num & (1 << i)
to check if the bit at that position is1
. - If
1
, increment the corresponding index inbitCount
.
- For each integer in
- Get Maximum Count:
- Iterate through
bitCount
to find the highest count. - This maximum count represents the largest subset size that can achieve a non-zero bitwise AND.
- Iterate through
- Return Result: Return this maximum value as the result.
- Initialize Count Array: Use an array
bitCount
initialized to 31 zeros, each representing a bit position's count of1
s. - Counting Set Bits:
- For each number in
candidates
:- Loop over each bit position from 0 to 30.
- Use
num & (1 << i)
to check if that bit position is1
. - If so, increment
bitCount
at that position.
- For each number in
- Get Maximum Count:
- Use
Math.max(...bitCount)
to find the maximum count inbitCount
. - This maximum value gives the size of the largest subset with a bitwise AND greater than zero.
- Use
- Return Result: Return this maximum value as the solution.
- Initialize Count Array: Create a list
bit_count
with 31 zeros to store counts for each bit position. - Counting Set Bits:
- For each number in
candidates
:- Iterate over bit positions from 0 to 30.
- Use
num & (1 << i)
to check if thei
-th bit is1
. - If true, increment
bit_count[i]
.
- For each number in
- Get Maximum Count:
- Use
max(bit_count)
to find the highest count inbit_count
. - This highest count represents the maximum subset size with a non-zero AND.
- Use
- Return Result: Return this maximum value.
- Initialize Count Array: Declare a fixed-size array
bitCount
of 31 elements, initialized to zero, for each bit position count. - Counting Set Bits:
- For each element in
candidates
:- Loop through each bit position from 0 to 30.
- Check if the bit is set using
num & (1 << i)
. - If set, increment
bitCount[i]
.
- For each element in
- Get Maximum Count:
- Find the highest value in
bitCount
by iterating through it. - This maximum value is the largest subset size that can achieve a non-zero AND.
- Find the highest value in
- Return Result: Return this maximum count.