Find the maximum possible Binary Number from given string | C ++ implementation of the approach ; Function to return maximum number that can be formed from the string ; To store the frequency of ' z ' and ' n ' in the given string ; Number of zeroes ; Number of ones ; To store the required number ; Add all the ones ; Add all the zeroes ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string maxNumber ( string str , int n ) { int freq [ 2 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' z ' ) { freq [ 0 ] ++ ; } else if ( str [ i ] == ' n ' ) { freq [ 1 ] ++ ; } } string num = " " ; for ( int i = 0 ; i < freq [ 1 ] ; i ++ ) num += '1' ; for ( int i = 0 ; i < freq [ 0 ] ; i ++ ) num += '0' ; return num ; } int main ( ) { string str = " roenenzooe " ; int n = str . length ( ) ; cout << maxNumber ( str , n ) ; return 0 ; }