# Minimum number of Binary strings to represent a Number

Given a number **N**. The task is to find the minimum number of binary strings required to represent the given number as the sum of the binary strings.

**Examples:**

Input :131

Output :Minimum Number of binary strings needed: 3

111 10 10

Input :564

Output :Minimum Number of binary strings needed: 6

111 111 111 111 110 10

**Approach: **

- Store all digits of the given number in the array.
- Find the maximum digit in the array. This maximum number(maxi) indicates the number of binary strings required to represent the given number.
- Now, find
**maxi**numbers by substuting 0’s and 1’s greadily.

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Python3

`# Python3 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 ` `def` `minBinary(n): ` ` ` `digit ` `=` `[` `0` `for` `i ` `in` `range` `(` `3` `)] ` ` ` `len` `=` `0` ` ` ` ` `while` `(n > ` `0` `): ` ` ` `digit[` `len` `] ` `=` `n ` `%` `10` ` ` `len` `+` `=` `1` ` ` `n ` `/` `/` `=` `10` ` ` ` ` `# Storing digits in correct order ` ` ` `digit ` `=` `digit[::` `-` `1` `] ` ` ` ` ` `ans ` `=` `0` ` ` ` ` `# Find the maximum digit in the array ` ` ` `for` `i ` `in` `range` `(` `len` `): ` ` ` `ans ` `=` `max` `(ans, digit[i]) ` ` ` ` ` `print` `(` `"Minimum Number of binary strings needed:"` `, ans) ` ` ` ` ` `# Traverse for all the binary strings ` ` ` `for` `i ` `in` `range` `(` `1` `, ans ` `+` `1` `, ` `1` `): ` ` ` `num ` `=` `0` ` ` `for` `j ` `in` `range` `(` `0` `, ` `len` `, ` `1` `): ` ` ` ` ` `# If digit at jth position is greater ` ` ` `# than 0 then substitute 1 ` ` ` `if` `(digit[j] > ` `0` `): ` ` ` `num ` `=` `num ` `*` `10` `+` `1` ` ` `digit[j] ` `-` `=` `1` ` ` `else` `: ` ` ` `num ` `*` `=` `10` ` ` `print` `(num, end ` `=` `" "` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `564` ` ` ` ` `minBinary(n) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

Minimum No of binary strings needed: 6 111 111 111 111 110 10

## Recommended Posts:

- Count number of binary strings without consecutive 1's
- Minimum number of pairs required to make two strings same
- Find the number of binary strings of length N with at least 3 consecutive 1s
- Number of Binary Strings of length N with K adjacent Set Bits
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Minimum number of 1's to be replaced in a binary array
- Minimum number of operations required to sum to binary string S
- Minimum sub-array such that number of 1's in concatenation of binary representation of its elements is at least K
- Minimum number of moves to make a binary array K periodic
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Find a number which give minimum sum when XOR with every number of array of integers
- Number of ways to split a binary number such that every part is divisible by 2
- Number of steps required to convert a binary number to one
- Maximum number of splits of a binary 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.