## Similar Problems

Similar Problems not available

# Largest Combination With Bitwise And Greater Than Zero - Leetcode Solution

## Companies:

LeetCode: Largest Combination With Bitwise And Greater Than Zero Leetcode Solution

Difficulty: Medium

Topics: hash-table bit-manipulation array

Problem Statement:

Given an integer array nums, return the largest subset of nums where every element of the subset is the bitwise AND of some of the other numbers in the subset. If there is no such subset, return an empty array.

Example 1:

Input: nums = [3,4,5,6,7,8] Output: [4,6,7,8] Explanation: The subset [4,6,7,8] is the largest subset where every element is the bitwise AND of others.

Solution:

The solution to this problem requires an understanding of how to determine a bitwise AND operation on a set of numbers in order to determine the largest subset of numbers that result in a non-zero value when the bitwise AND operation is applied to all elements in the set.

In order to determine the largest combination of numbers that result in a non-zero value when the bitwise AND operation is applied to all elements in the set, we must iterate through the array and determine the elements that share a non-zero bit with the previous element by applying the bitwise AND operator.

If the current element ANDed with the previous element is greater than zero, we add both the current and previous elements to a candidate set.

Once we have added all possible candidates to the candidate set, we iterate through all possible combinations of two elements from the candidate set. We will then compute the bitwise AND operation on both elements in the combination to determine if the result is greater than zero. If the result of the bitwise AND operation is greater than zero, then this subset is a valid subset candidate.

We will then iterate through all possible subsets of candidate sets with size greater than or equal to two to determine the largest subset of numbers that result in a non-zero value when the bitwise AND operation is applied to all elements in the subset.

Below is the implementation of the solution:

```
class Solution:
def largestAnd(self, nums: List[int]) -> List[int]:
def get_possible_candidates(prev, curr):
res = [curr]
while curr > 0 and (curr & prev) != prev:
curr &= curr - 1
res.append(curr)
return res
n = len(nums)
candidates = set()
for i in range(n):
for candidate in get_possible_candidates(i and nums[i - 1], nums[i]):
candidates.add(candidate)
candidates = sorted(list(candidates), key=lambda x: bin(x).count('1'), reverse=True)
for i in range(len(candidates)):
subset = [candidates[i]]
for j in range(i + 1, len(candidates)):
if all(subset[k] & candidates[j] for k in range(len(subset))):
subset.append(candidates[j])
if len(subset) > 1 and all(subset[k] & subset[k - 1] for k in range(1, len(subset))):
return subset
return []
```

Time Complexity:

The above solution will take O(2^n) time complexity for generating all possible subsets of candidates.

Space Complexity:

The above solution will take O(n) auxiliary space to generate the candidate subset.

## Largest Combination With Bitwise And Greater Than Zero Solution Code

`1`