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
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.
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Numbers whose bitwise OR and sum with N are equal
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Find subsequences with maximum Bitwise AND and Bitwise OR
- 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 numbers whose maximum sum of distinct digit-sum is less than or equals M
- Find two numbers whose difference of fourth power is equal to N
- Count distinct Bitwise OR of all Subarrays
- Count of pairs whose bitwise AND is a power of 2
- Count numbers whose sum with x is equal to XOR with x
- Count numbers whose difference with N is equal to XOR with N
- Count numbers whose XOR with N is equal to OR with N
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Find any K distinct odd integers such that their sum is equal to N
- Maximum subset with bitwise OR equal to k
- Largest set with bitwise OR equal to n
- Minimum Bitwise OR operations to make any two array elements 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 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.