Given a binary number, the task is to remove exactly one bit from it such that, after it’s removal, the resultant binary number is greatest from all the options.
Input: 110 Output: 11 As 110 = 6 in decimal, the option is to remove either 0 or 1. So the possible combinations are 10, 11 The max number is 11 = 3 in decimal. Input:1001 Output: 101
- Traverse the binary number from left to right.
- Find the least redundant 0 bit, as this bit will have the least effect on the resultant binary number.
- Skip this bit, or remove it.
- The rest of the bits give the maximum value binary number.
Below is the implementation of the above approach:
Time Complexity: O(n)
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 bit flips such that every K consecutive bits contain at least one set bit
- Count of 1-bit and 2-bit characters in the given binary string
- Next greater number than N with exactly one bit different in binary representation of N
- Arrange a binary string to get maximum value within a range of indices
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Find value of k-th bit in binary representation
- Ways to remove one element from a binary string so that XOR becomes zero
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Get the position of rightmost unset bit
- Check whether the two numbers differ at one bit position only
- Count of numbers having only one unset bit in a range [L,R]
- Meta Binary Search | One-Sided Binary Search
- Number of steps required to convert a binary number to one
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum consecutive one’s (or zeros) in a binary circular array
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Position of rightmost bit with first carry in sum of two binary
- Minimum bit changes in Binary Circular array to reach a index
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.