Check if bitwise AND of any subset is power of two in Python

Suppose we have an array of numbers called nums. We need to check whether there exists any subset of nums whose bitwise AND is a power of two or not.

So, if the input is like nums = [22, 25, 9], then the output will be True, as subset {22, 9} has binary forms {10110, 1001} and their AND is 10000 = 16, which is a power of 2.

Understanding the Problem

A number is a power of 2 if it has only one bit set (like 1, 2, 4, 8, 16, etc.). We can check this using the formula: n & (n-1) == 0 where n > 0.

Algorithm Steps

To solve this, we follow these steps ?

  • MAX := 32 (considering 32-bit numbers at maximum)
  • Define a helper function to check if a number is power of 2
  • If array has only one element, check if it's a power of 2
  • For each bit position, find all numbers that have that bit set
  • Calculate AND of those numbers and check if result is power of 2

Implementation

MAX = 32

def is_power_of_2(v):
    return v and (v & (v - 1)) == 0

def solve(nums):
    if len(nums) == 1:
        return is_power_of_2(nums[0])
    
    total = 0
    for i in range(0, MAX):
        total = total | (1 << i)
    
    for i in range(0, MAX):
        ret = total
        for j in range(0, len(nums)):
            if nums[j] & (1 << i):
                ret = ret & nums[j]
        if is_power_of_2(ret):
            return True
    
    return False

# Test the function
nums = [22, 25, 9]
print(solve(nums))
True

How It Works

Let's trace through the example with nums = [22, 25, 9]:

  • 22 in binary: 10110
  • 25 in binary: 11001
  • 9 in binary: 01001

The algorithm checks each bit position. For bit position 4 (16's place), both 22 and 9 have this bit set. Their AND operation: 10110 & 01001 = 00000, but we start with total and AND with selected numbers, giving us 16 (10000), which is a power of 2.

Alternative Approach

Here's a more intuitive approach that generates all possible subsets ?

def is_power_of_2(n):
    return n > 0 and (n & (n - 1)) == 0

def check_subsets_bitwise_and(nums):
    n = len(nums)
    
    # Generate all possible non-empty subsets
    for i in range(1, 2**n):
        subset_and = None
        
        # Calculate AND of current subset
        for j in range(n):
            if i & (1 << j):
                if subset_and is None:
                    subset_and = nums[j]
                else:
                    subset_and &= nums[j]
        
        # Check if AND result is power of 2
        if is_power_of_2(subset_and):
            return True
    
    return False

# Test with example
nums = [22, 25, 9]
print(check_subsets_bitwise_and(nums))
True

Conclusion

Both approaches check if any subset's bitwise AND is a power of 2. The first method is more efficient for larger inputs, while the second is more intuitive by explicitly generating subsets.

Updated on: 2026-03-25T14:24:36+05:30

458 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements