Check whether bitwise AND of a number with any subset of an array is zero or not
Given an array and a Number N. The task is to check whether there exists any subset of this array such that the bitwise AND of this subset with N is zero.
Examples:
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
A Simple Approach is to find the bitwise AND of all the subsets of the array and check whether the AND of N with any subset is zero or not.
An Efficient Approach is to observe that the bitwise-AND of any two numbers will always produce a number less than or equal to the smaller number. So our task is to find the subset which has the minimum value of bitwise AND. So as stated earlier the AND of any two numbers will always produce a number less than or equal to the minimum number so the minimum value of AND will be the AND of all the array elements. So the task now reduces to check the bitwise-AND of all the array elements and N and if it is zero we will print YES otherwise NO.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isSubsetAndZero( int array[], int length, int N)
{
int arrAnd = array[0];
for ( int i = 1; i < length; i++) {
arrAnd = arrAnd & array[i];
}
if ((arrAnd & N) == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
int main()
{
int array[] = { 1, 2, 4 };
int length = sizeof (array) / sizeof ( int );
int N = 3;
isSubsetAndZero(array, length, N);
}
|
Java
import java.io.*;
public class GFG {
static void isSubsetAndZero( int array[], int length, int N)
{
int arrAnd = array[ 0 ];
for ( int i = 1 ; i < length; i++) {
arrAnd = arrAnd & array[i];
}
if ((arrAnd & N) == 0 )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
public static void main (String[] args) {
int array[] = { 1 , 2 , 4 };
int length = array.length;
int N = 3 ;
isSubsetAndZero(array, length, N);
}
}
|
Python 3
def isSubsetAndZero(array, length, N):
arrAnd = array[ 0 ]
for i in range ( 1 , length) :
arrAnd = arrAnd & array[i]
if ((arrAnd & N) = = 0 ):
print ( "YES" )
else :
print ( "NO" )
if __name__ = = "__main__" :
array = [ 1 , 2 , 4 ]
length = len (array)
N = 3
isSubsetAndZero(array, length, N)
|
C#
using System;
class GFG
{
static void isSubsetAndZero( int []array,
int length, int N)
{
int arrAnd = array[0];
for ( int i = 1; i < length; i++)
{
arrAnd = arrAnd & array[i];
}
if ((arrAnd & N) == 0)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
public static void Main ()
{
int []array = { 1, 2, 4 };
int length = array.Length;
int N = 3;
isSubsetAndZero(array, length, N);
}
}
|
PHP
<?php
function isSubsetAndZero( $array , $length , $N )
{
$arrAnd = $array [0];
for ( $i = 1; $i < $length ; $i ++)
{
$arrAnd = $arrAnd & $array [ $i ];
}
if (( $arrAnd & $N ) == 0)
echo ( "YES" );
else
echo ( "NO" );
}
$array = array ( 1, 2, 4 );
$length = count ( $array );
$N = 3;
isSubsetAndZero( $array , $length , $N );
?>
|
Javascript
<script>
function isSubsetAndZero(array, len, N)
{
var arrAnd = array[0];
for ( var i = 1; i < len; i++) {
arrAnd = arrAnd & array[i];
}
if ((arrAnd & N) == 0)
document.write( "YES" + "<br>" );
else
document.write( "NO" + "<br>" );
}
var array = [1, 2, 4 ];
var len = array.length;
var N = 3;
isSubsetAndZero(array, len, N);
</script>
|
Time Complexity: O(N), where N is the length of the array
Auxiliary Space: O(1)
Last Updated :
29 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...