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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Size of the smallest subset with maximum Bitwise OR
- Maximum sum subset having equal number of positive and negative elements
- 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
- Largest subset having with sum less than equal to sum of respective indices
- Numbers whose bitwise OR and sum with N are equal
- Largest set with bitwise OR equal to n
- Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
- Count of pairs from Array with sum equal to twice their bitwise AND
- Find N distinct numbers whose bitwise Or is equal to K
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Find the maximum subset XOR of a given set
- Minimum Bitwise OR operations to make any two array elements equal
- Maximum size subset with given sum using Backtracking
- Maximum subset sum such that no two elements in set have same digit in them
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Maximum Bitwise OR pair from a range
- Maximum possible Bitwise OR of the two numbers from the range [L, R]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.