Remove one bit from a binary number to get maximum value | C ++ program to find next maximum binary number with one bit removed ; Function to find the maximum binary number ; Traverse the binary number ; Try finding a 0 and skip it ; Driver code ; Get the binary number ; Find the maximum binary number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printMaxAfterRemoval ( string s ) { bool flag = false ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' && flag == false ) { flag = true ; continue ; } else cout << s [ i ] ; } } int main ( ) { string s = "1001" ; printMaxAfterRemoval ( s ) ; }