Given an array of non negative integers and an integer k, find the subset of maximum length with bitwise OR equal to k.
Input : arr = [1, 4, 2] k = 3 Output : [1, 2] Explanation: The bitwise OR of 1 and 2 equals 3. It is not possible to obtain a subset of length greater than 2. Input : arr = [1, 2, 5] k = 4 Output :  No subset's bitwise OR equals 4.
The naive method would be to consider all the subsets. While considering a subset, compute its bitwise OR. If it equals k, compare the subset’s length with the maximum length so far and update the maximum length if required.
0 OR 0 = 0
1 OR 0 = 1
1 OR 1 = 1
Hence, for all the positions in the binary representation of k with the bit equal to 0, the corresponding position in the binary representations of all the elements in the resulting subset should necessarily be 0.
On the other hand, for positions in k with the bit equal to 1, there has to be at least one element with a 1 in the corresponding position. Rest of the elements can have either 0 or 1 in that position, it does not matter.
Therefore, to obtain the resulting subset, traverse the initial array. While deciding if the element should be in the resulting subset or not, check whether there is any position in the binary representation of k which is 0 and the corresponding position in that element is 1. If there exists such a position, then ignore that element, else include it in the resulting subset.
How to determine if there exists a position in the binary representation of k which is 0 and the corresponding position in an element is 1?
Simply take bitwise OR of k and that element. If it does not equal to k, then there exists such a position and the element has to be ignored. If their bitwise OR equals to k, then include the current element in the resulting subset.
The final step is to determine if there is at least one element with a 1 in a position with 1 in the corresponding position in k.
Simply compute the bitwise OR of the resulting subset. If it equals to k, then this is the final answer. Else no subset exists which satisfies the condition.
Time complexity : O(N), where N is the size of array.
- Size of the smallest subset with maximum Bitwise OR
- Check if bitwise AND of any subset is power of two
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Numbers whose bitwise OR and sum with N are equal
- Largest set with bitwise OR equal to n
- Find N distinct numbers whose bitwise Or is equal to K
- Find the maximum subset XOR of a given set
- Minimum Bitwise OR operations to make any two array elements equal
- Maximum subset sum such that no two elements in set have same digit in them
- Maximum Bitwise AND pair from given range
- Maximum steps to transform 0 to X with bitwise AND
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Subset Sum | Backtracking-4
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.