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
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.
- Numbers whose bitwise OR and sum with N are equal
- Find any K distinct odd integers such that their sum is equal to N
- Find two distinct prime numbers with given product
- Find the count of distinct numbers in a range
- 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 K numbers with sum equal to N and sum of their squares maximized
- Find two numbers such that difference of their squares equal to N
- Find maximum product of digits among numbers less than or equal to N
- Find two numbers whose difference of fourth power is equal to N
- Largest set with bitwise OR equal to n
- Maximum subset with bitwise OR equal to k
- Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count of pairs from Array with sum equal to twice their bitwise AND
- Minimum Bitwise OR operations to make any two array elements equal
- Distinct powers of a number N such that the sum is equal to K
- Numbers less than N which are product of exactly two distinct prime numbers
- Check if the sum of distinct digits of two integers are equal
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.