Minimum number of Binary strings to represent a Number | C ++ program to find the minimum number of binary strings to represent a number ; Function to find the minimum number of binary strings to represent a number ; Storing digits in correct order ; Find the maximum digit in the array ; Traverse for all the binary strings ; If digit at jth position is greater than 0 then substitute 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minBinary ( int n ) { int digit [ 10 ] , len = 0 ; while ( n > 0 ) { digit [ len ++ ] = n % 10 ; n /= 10 ; } reverse ( digit , digit + len ) ; int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { ans = max ( ans , digit [ i ] ) ; } cout << " Minimum ▁ Number ▁ of ▁ binary ▁ strings ▁ needed : ▁ " << ans << endl ; for ( int i = 1 ; i <= ans ; i ++ ) { int num = 0 ; for ( int j = 0 ; j < len ; j ++ ) { if ( digit [ j ] > 0 ) { num = num * 10 + 1 ; digit [ j ] -- ; } else { num *= 10 ; } } cout << num << " ▁ " ; } } int main ( ) { int n = 564 ; minBinary ( n ) ; return 0 ; }