Given two integers N and K, the task is to find N distinct integers whose bit-wise OR is equal to K. If there does not exist any possible answer then print -1.
Input: N = 3, K = 5
Output: 5 0 1
5 OR 0 OR 1 = 5
Input: N = 10, K = 5
It is not possible to find any solution.
- We know that if bit-wise OR of a sequence of numbers is K then all the bit positions which are 0 in K must also be zero in all the numbers.
- So, we only have those positions to change where bit is 1 in K. Say that count is Bit_K.
- Now, we can form pow(2, Bit_K) distinct numbers with Bit_K bits. So if, we set one number to be K itself, then rest N – 1 numbers can be built by setting 0 all the bits in every number which are 0 in K and for other bit positions any permutation of Bit_K bits other than number K.
- If pow(2, Bit_K) < N then we cannot find any possible answer.
Below is the implementation of the above approach:
5 0 1
- Numbers whose bitwise OR and sum with N are equal
- Find two distinct prime numbers with given product
- Count distinct Bitwise OR of all Subarrays
- Find all factorial numbers less than or equal to n
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Find maximum product of digits among numbers less than or equal to N
- Largest set with bitwise OR equal to n
- Maximum subset with bitwise OR equal to k
- Minimum Bitwise OR operations to make any two array elements equal
- Check if the sum of distinct digits of two integers are equal
- Numbers less than N which are product of exactly two distinct prime numbers
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Check whether bitwise OR of N numbers is Even or Odd
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Numbers that are bitwise AND of at least one non-empty sub-array
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.