Remove one bit from a binary number to get maximum value
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.
Examples:
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
Approach:
- 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:
Program:
C++
// C++ program to find next maximum binary number // with one bit removed #include <bits/stdc++.h> using namespace std; // Function to find the maximum binary number int printMaxAfterRemoval(string s) { bool flag = false; int n = s.length(); // Traverse the binary number for (int i = 0; i < n; i++) { // Try finding a 0 and skip it if (s[i] == '0' && flag == false) { flag = true; continue; } else cout << s[i]; } } // Driver code int main() { // Get the binary number string s = "1001"; // Find the maximum binary number printMaxAfterRemoval(s); } |
Java
// Java program to find next maximum binary number // with one bit removed import java.io.*; class GFG { // Function to find the maximum binary number static int printMaxAfterRemoval(String s) { boolean flag = false; int n = s.length(); // Traverse the binary number for (int i = 0; i < n; i++) { // Try finding a 0 and skip it if (s.charAt(i) == '0' && flag == false) { flag = true; continue; } else System.out.print( s.charAt(i)); } return 0; } // Driver code public static void main (String[] args) { // Get the binary number String s = "1001"; // Find the maximum binary number printMaxAfterRemoval(s); } } // This code is contributed by anuj_67.. |
Python3
# Python3 program to find next maximum
# binary number with one bit removed
# Function to find the maximum
# binary number
def printMaxAfterRemoval(s):
flag = False
n = len(s)
# Traverse the binary number
for i in range(0, n):
# Try finding a 0 and skip it
if s[i] == ‘0’ and flag == False:
flag = True
continue
else:
print(s[i], end = “”)
# Driver code
if __name__ == “__main__”:
# Get the binary number
s = “1001”
# Find the maximum binary number
printMaxAfterRemoval(s)
# This code is contributed
# by Rituraj Jain
C#
// C# program to find next maximum // binary number with one bit removed using System; class GFG { // Function to find the maximum // binary number static int printMaxAfterRemoval(String s) { bool flag = false; int n = s.Length; // Traverse the binary number for (int i = 0; i < n; i++) { // Try finding a 0 and skip it if (s[i] == '0' && flag == false) { flag = true; continue; } else Console.Write(s[i]); } return 0; } // Driver Code static void Main() { // Get the binary number String s = "1001"; // Find the maximum binary number printMaxAfterRemoval(s); } } // This code is contributed by Ryuga. |
PHP
<?php // PHP program to find next maximum // binary number with one bit removed // Function to find the maximum // binary number function printMaxAfterRemoval($s) { $flag = false; $n = strlen($s); // Traverse the binary number for ($i = 0; $i < $n; $i++) { // Try finding a 0 and skip it if ($s[$i] == '0' && $flag == false) { $flag = true; continue; } else echo $s[$i]; } } // Driver code // Get the binary number $s = "1001"; // Find the maximum binary number printMaxAfterRemoval($s); // This code is contributed // by Akanksha Rai ?> |
101
Time Complexity: O(n)
Recommended Posts:
- Ways to remove one element from a binary string so that XOR becomes zero
- Minimum steps to remove substring 010 from a binary string
- Maximum 0's between two immediate 1's in binary representation
- Maximum difference of zeros and ones in binary string
- Maximum contiguous 1 possible in a binary string after k rotations
- Maximum consecutive one’s (or zeros) in a binary array
- Minimum digits to remove to make a number Perfect Square
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Arrange a binary string to get maximum value within a range of indices
- Maximum consecutive one’s (or zeros) in a binary circular array
- Maximum Consecutive Zeroes in Concatenated Binary String
- Count number of trailing zeros in Binary representation of a number using Bitset
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Minimum number of Binary strings to represent a Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



