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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Size of the smallest subset with maximum Bitwise OR
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- 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
- Maximum sum subset having equal number of positive and negative elements
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Largest subset having with sum less than equal to sum of respective indices
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- 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
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.