📜  检查任何子集的按位与是否为2的幂

📅  最后修改于: 2021-05-04 08:29:48             🧑  作者: Mango

给定一个nrr正整数的数组arr [] 。任务是检查数组的任何子集的按位与是2的幂(即1、2、4、8、16…)。

例子:

Input : n = 3, arr[] = { 12, 13, 7 }
Output : Yes
Subset { 12, 7 } has Biwise AND value 4, which 
is power of 2.

Input : n = 2, arr[] = { 10, 20 }
Output : No

注意,要使数字为2的幂,它应该只有1个置1的位。
如果n为1,那么我们只需检查数字是否只有唯一的一位。
对于n大于1,我们的任务是从数组中选择那些按位与导致仅单个位集编号的数字。为此,我们搜索一个位置,在该位置集合中的所有元素都在该位置设置了一位。例如,对于集合{4(100),6(110),7(111)},在位置2(从右到左,从0开始的索引)中,为所有元素设置了位。因此,按位与运算得到4,即2的幂。

以下是此方法的实现:

C++
// CPP Program to check if Bitwise AND of any 
// subset is power of two
#include 
using namespace std;
  
const int NUM_BITS = 32;
  
// Check for power of 2 or not
bool isPowerOf2(int num)
{
    return (num && !(num & (num - 1)));
}
  
// Check if there exist a subset whose bitwise AND
// is power of 2.
bool checkSubsequence(int arr[], int n)
{
    // if there is only one element in the set.
    if (n == 1) 
       return isPowerOf2(arr[0]);
  
    // Finding a number with all bit sets.
    int total = 0;
    for (int i = 0; i < NUM_BITS; i++)
        total = total | (1 << i);
  
    // check all the positions at which the bit is set.
    for (int i = 0; i < NUM_BITS; i++) {
  
        int ans = total;
        for (int j = 0; j < n; j++) {
  
            // include all those elements whose 
            // i-th bit is set
            if (arr[j] & (1 << i))
                ans = ans & arr[j];
        }
  
        // check for the set contains elements 
        // make a power of 2 or not
        if (isPowerOf2(ans)) 
            return true;
    }
    return false;
}
  
// Driver Program
int main()
{
    int arr[] = { 12, 13, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    if (checkSubsequence(arr, n))
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


Java
// Java Program to check if Bitwise AND of any 
// subset is power of two
import java.io.*;
import java.util.*;
  
public class GFG {
       
    static int NUM_BITS = 32;
       
    // Check for power of 2 or not
    static boolean isPowerOf2(int num)
    {
        if(num != 0 && (num & (num - 1)) == 0)
            return true;
        return false;
    }
       
    // Check if there exist a
    // subset whose bitwise AND
    // is power of 2.
    static boolean checkSubsequence(int []arr, int n)
    {
           
        // if there is only one 
        // element in the set.
        if (n == 1) 
            return isPowerOf2(arr[0]);
       
        // Finding a number with
        // all bit sets.
        int total = 0;
        for (int i = 0; i < NUM_BITS; i++)
            total = total | (1 << i);
       
        // check all the positions 
        // at which the bit is set.
        for (int i = 0; i < NUM_BITS; i++)
        {
       
            int ans = total;
            for (int j = 0; j < n; j++) 
            {
       
                // include all those 
                // elements whose 
                // i-th bit is set
                int p = arr[j] & (1 << i); 
                if (p == 0)
                    ans = ans & arr[j];
            }
       
            // check for the set
            // contains elements 
            // make a power of 2
            // or not
            if (isPowerOf2(ans)) 
                return true;
        }
        return false;
    }
       
    // Driver Code
    public static void main(String args[])
    {
        int []arr = {12, 13, 7};
        int n = arr.length;
        if (checkSubsequence(arr, n))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
   
// This code is contributed by 
// Manish Shaw (manishshaw1)


Python3
# Python3 Program to check if Bitwise AND of any 
# subset is power of two
   
NUM_BITS = 32
   
# Check for power of 2 or not
def isPowerOf2(num):
    return (num and (num & (num - 1)) == 0)
   
# Check if there exist a subset whose bitwise AND
# is power of 2.
def checkSubsequence(arr, n):
  
    # if there is only one element in the set.
    if (n == 1):
        return isPowerOf2(arr[0])
   
    # Finding a number with all bit sets.
    total = 0
    for i in range(0, NUM_BITS):
        total = total | (1 << i)
   
    # check all the positions at which the bit is set.
    for i in range(0, NUM_BITS): 
   
        ans = total
        for j in range(0, n):
  
            # include all those elements whose 
            # i-th bit is set
            if (arr[j] & (1 << i)):
                ans = ans & arr[j]
   
        # check for the set contains elements 
        # make a power of 2 or not
        if (isPowerOf2(ans)):
            return True
    return False
   
# Driver Program
arr = [ 12, 13, 7 ]
n = len(arr)
if (checkSubsequence(arr, n)):
    print ("YES\n")
else:
    print ("NO\n")
  
# This code is contributed by Manish Shaw
# (manishshaw1)


C#
// C# Program to check if Bitwise AND of any 
// subset is power of two
using System;
using System.Collections.Generic;
  
class GFG {
      
    static int NUM_BITS = 32;
      
    // Check for power of 2 or not
    static bool isPowerOf2(int num)
    {
        if(num != 0 && (num & (num - 1)) == 0)
            return true;
        return false;
    }
      
    // Check if there exist a
    // subset whose bitwise AND
    // is power of 2.
    static bool checkSubsequence(int []arr, int n)
    {
          
        // if there is only one 
        // element in the set.
        if (n == 1) 
            return isPowerOf2(arr[0]);
      
        // Finding a number with
        // all bit sets.
        int total = 0;
        for (int i = 0; i < NUM_BITS; i++)
            total = total | (1 << i);
      
        // check all the positions 
        // at which the bit is set.
        for (int i = 0; i < NUM_BITS; i++)
        {
      
            int ans = total;
            for (int j = 0; j < n; j++) 
            {
      
                // include all those 
                // elements whose 
                // i-th bit is set
                int p = arr[j] & (1 << i); 
                if (p == 0)
                    ans = ans & arr[j];
            }
      
            // check for the set
            // contains elements 
            // make a power of 2
            // or not
            if (isPowerOf2(ans)) 
                return true;
        }
        return false;
    }
      
    // Driver Code
    public static void Main()
    {
        int []arr = {12, 13, 7};
        int n = arr.Length;
        if (checkSubsequence(arr, n))
            Console.Write("YES\n");
        else
            Console.Write("NO\n");
    }
}
  
// This code is contributed by 
// Manish Shaw (manishshaw1)


PHP


输出:
YES


参考:

https://stackoverflow.com/questions/35990794/subset-of-array-a-in-which-if-we-do-and-of-all-elements-of-that-subset-then-outp