Given two numbers n and k, we need to find the minimum number of flips required to maximize given number by flipping its bits such that the resulting number has exactly k set bits.
Note : K must be less than number of bits in n.
Input : n = 14, k = 2 Output : Min Flips = 1 Explanation : Binary representation of 14 = 1110 Largest 4-digit Binary number with 2 set bit = 1100 Conversion from 1110 to 1100 requires 1 flipping Input : n = 145, k = 4 Output : Min Flips = 3 Explanation : Binary representation of 145 = 10010001 Largest 8-digit Binary number with 4 set bit = 11110000 Conversion from 10010001 to 11110000 requires 3 flipping
For the given number n and k find the largest number possible with k-set bits and having exactly same number of bits as n has as :
- size = log2(n) + 1 gives the number of bits of n.
- max = pow(2, k) – 1 gives largest possible number with k bits.
- max = max << (size – k) gives the largest number possible with k-set bits and having exactly same number of bits as n has
- Number of set bit in (n XOR max ) is our required number of flipping.
Illustration of above approach :
let n = 145 (10010001), k = 4 size = log2(n) + 1 = log2(145) + 1 = 7 + 1 = 8 max = pow(2, k) -1 = pow(2, 4) - 1 = 16 - 1 = 15 (1111) max = max << (size - k) = 15 << (8 - 4) = 240 (11110000) number of set bit in = no. of set bit in (n XOR max ) (145 ^ 240 ) = 3
Min Flips = 3
This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Minimum flips required to keep all 1s together in a Binary string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Maximize the number by rearranging bits
- Maximize the number by flipping at most K bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Minimum number of given powers of 2 required to represent a number
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Minimum number of operations required to sum to binary string S
- Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)
- Minimum number using set bits of a given number
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Maximize count of set bits in a root to leaf path in a binary tree
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Count minimum bits to flip such that XOR of A and B equal to C
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Minimum swaps required to convert one binary string to another
- Find the largest number smaller than integer N with maximum number of set bits