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 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 that 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++
// C++ program to check whether bitwise AND of a number // with any subset of an array is zero or not #include <bits/stdc++.h> 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
<?php // PHP 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 function 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 = 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) echo("YES"); else echo("NO"); } // Driver Code $array = array( 1, 2, 4 ); $length = count($array); $N = 3; isSubsetAndZero($array, $length, $N); // This code is contributed // by Shashank ?> |
YES
Recommended Posts:
- Check if bitwise AND of any subset is power of two
- Check if a number is multiple of 9 using bitwise operators
- Check if a number is divisible by 17 using bitwise operators
- Check if a number is divisible by 8 using bitwise operators
- Maximum subset with bitwise OR equal to k
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Check whether bitwise AND of N numbers is Even or Odd
- Check whether bitwise OR of N numbers is Even or Odd
- Find whether an array is subset of another array using Map
- Array with GCD of any of its subset belongs to the given array
- Subset array sum by generating all the subsets
- Largest divisible subset in array
- Minimum value among AND of elements of every subset of an array
- Find if there is any subset of size K with 0 sum in an array of -1 and +1
- Check in binary array the number represented by a subarray is odd or even
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



