📌  相关文章
📜  检查数字与数组的任何子集的按位与运算是否为零

📅  最后修改于: 2021-06-26 12:43:27             🧑  作者: Mango

给定一个数组和一个数字N。任务是检查此数组是否存在任何子集,以使该子集与N的按位与为零。
例子

Input : arr[] = {1, 2, 4}  ;  N = 3
Output : YES
Explanation: The subsets are:
(1, 2 ), (1, 4), (1, 2, 4) 

Input : arr[] = {1, 1, 1}  ;  N = 3
Output : NO

一种简单的方法是找到数组所有子集的按位与,并检查N与任何子集的与是否为零。
一种有效的方法是观察任何两个数字的按位与运算将始终产生小于或等于较小数字的数字。因此,我们的任务是找到具有按位AND最小值的子集。因此,如前所述,任何两个数字的AND总是会产生一个小于或等于最小值的数字,因此AND的最小值将是所有数组元素的AND。因此,现在任务减少了,以检查所有数组元素和N的按位与,如果为零,我们将打印YES,否则显示NO。
下面是上述方法的实现:

C++
// C++ program to check whether bitwise AND of a number
// with any subset of an array is zero or not
#include 
using namespace std;
 
// Function to check whether bitwise AND of a number
// with any subset of an array is zero or not
void isSubsetAndZero(int array[], int length, int N)
{
    // variable to store the
    // AND of all the elements
    int arrAnd = array[0];
 
    // find the AND of all the elements
    // of the array
    for (int i = 1; i < length; i++) {
        arrAnd = arrAnd & array[i];
    }
 
    // if the AND of all the array elements
    // and N is equal to zero
    if ((arrAnd & N) == 0)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
 
// Driver Code
int main()
{
    int array[] = { 1, 2, 4 };
    int length = sizeof(array) / sizeof(int);
 
    int N = 3;
 
    isSubsetAndZero(array, length, N);
}


Java
// Java program to check whether bitwise AND of a number
// with any subset of an array is zero or not
import java.io.*;
 
public class GFG {
 
 
// Function to check whether bitwise AND of a number
// with any subset of an array is zero or not
static void isSubsetAndZero(int array[], int length, int N)
{
    // variable to store the
    // AND of all the elements
    int arrAnd = array[0];
 
    // find the AND of all the elements
    // of the array
    for (int i = 1; i < length; i++) {
        arrAnd = arrAnd & array[i];
    }
 
    // if the AND of all the array elements
    // and N is equal to zero
    if ((arrAnd & N) == 0)
        System.out.println( "YES");
    else
        System.out.println( "NO");
}
 
// Driver Code
    public static void main (String[] args) {
        int array[] = { 1, 2, 4 };
    int length = array.length;
 
    int N = 3;
 
    isSubsetAndZero(array, length, N);
    }
}
//This code is contributed by shs..


Python 3
# Python 3 program to check whether
# bitwise AND of a number with any
# subset of an array is zero or not
 
# Function to check whether bitwise
# AND of a number with any subset
# of an array is zero or not
def isSubsetAndZero(array, length, N):
 
    # variable to store the
    # AND of all the elements
    arrAnd = array[0]
 
    # find the AND of all
    # the elements of the array
    for i in range(1, length) :
        arrAnd = arrAnd & array[i]
 
    # if the AND of all the array
    # elements and N is equal to zero
    if ((arrAnd & N) == 0):
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == "__main__":
    array = [ 1, 2, 4 ]
    length = len(array)
 
    N = 3
 
    isSubsetAndZero(array, length, N)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to check whether
// bitwise AND of a number with
// any subset of an array is zero or not
using System;
 
class GFG
{
 
// Function to check whether bitwise
// AND of a number with any subset
// of an array is zero or not
static void isSubsetAndZero(int []array,
                            int length, int N)
{
    // variable to store the
    // AND of all the elements
    int arrAnd = array[0];
 
    // find the AND of all the
    // elements of the array
    for (int i = 1; i < length; i++)
    {
        arrAnd = arrAnd & array[i];
    }
 
    // if the AND of all the array
    // elements and N is equal to zero
    if ((arrAnd & N) == 0)
        Console.WriteLine( "YES");
    else
        Console.WriteLine( "NO");
}
 
// Driver Code
public static void Main ()
{
    int []array = { 1, 2, 4 };
    int length = array.Length;
     
    int N = 3;
     
    isSubsetAndZero(array, length, N);
}
}
 
// This code is contributed
// by inder_verma


PHP


Javascript


输出:
YES

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。