# Represent a number as sum of minimum possible psuedobinary numbers

Given a number, you have to represent this number as sum of minimum number of possible *psuedobinary* numbers. A number is said to be *psuedobinary* number if its decimal number consists of only two digits (0 and 1). Example: 11,10,101 are all psuedobinary numbers.

**Examples :-**

Input : 44 Output : 11 11 11 11 Explanation : 44 can be represented as sum of minimum 4 psuedobinary numbers as 11+11+11+11 Input : 31 Output : 11 10 10 Explanation : 31 can be represented as sum of minimum 3 psuedobinary numbers as 11+10+10

The idea to do this is to first observe carefully that we need to calculate minimum number of possible psuedobinary numbers. To do this we find a new number m such that if for a place in given number n, the digit is non-zero then the digit in that place in m is 1 otherwise zero. For example if n = 5102, then m will be 1101. Then we will print this number m and subtract m from n. We will keep repeating these steps until n is greater than zero.

## C++

`// C++ program to represent a given ` `// number as sum of minimum possible ` `// psuedobinary numbers ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `// function to represent a given ` `// number as sum of minimum possible ` `// psuedobinary numbers ` `void` `psuedoBinary(` `int` `n) ` `{ ` ` ` `// Repeat below steps until n > 0 ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `// calculate m (A number that has same ` ` ` `// number of digits as n, but has 1 in ` ` ` `// place of non-zero digits 0 in place ` ` ` `// of 0 digits) ` ` ` `int` `temp = n, m = 0, p = 1; ` ` ` `while` `(temp) ` ` ` `{ ` ` ` `int` `rem = temp % 10; ` ` ` `temp = temp / 10; ` ` ` ` ` `if` `(rem != 0) ` ` ` `m += p; ` ` ` ` ` `p *= 10; ` ` ` `} ` ` ` ` ` `cout << m << ` `" "` `; ` ` ` ` ` `// subtract m from n ` ` ` `n = n - m; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 31; ` ` ` ` ` `psuedoBinary(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to represent a given ` `// number as sum of minimum possible ` `// psuedobinary numbers ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `void` `psuedoBinary(` `int` `n) ` ` ` `{ ` ` ` `// Repeat below steps until n > 0 ` ` ` `while` `(n != ` `0` `) ` ` ` `{ ` ` ` `// calculate m (A number that has same ` ` ` `// number of digits as n, but has 1 in ` ` ` `// place of non-zero digits 0 in place ` ` ` `// of 0 digits) ` ` ` `int` `temp = n, m = ` `0` `, p = ` `1` `; ` ` ` `while` `(temp != ` `0` `) ` ` ` `{ ` ` ` `int` `rem = temp % ` `10` `; ` ` ` `temp = temp / ` `10` `; ` ` ` ` ` `if` `(rem != ` `0` `) ` ` ` `m += p; ` ` ` ` ` `p *= ` `10` `; ` ` ` `} ` ` ` ` ` `System.out.print(m + ` `" "` `); ` ` ` ` ` `// subtract m from n ` ` ` `n = n - m; ` ` ` `} ` ` ` `System.out.println(` `" "` `); ` ` ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `31` `; ` ` ` `psuedoBinary(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Mohit Gupta_OMG ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to represent ` `# a given number as sum of ` `# minimum possible psuedobinary ` `# numbers ` ` ` `# function to represent a ` `# given number as sum of ` `# minimum possible ` `# psuedobinary numbers ` `def` `psuedoBinary(n): ` ` ` ` ` `# Repeat below steps ` ` ` `# until n > 0 ` ` ` `while` `(n > ` `0` `): ` ` ` ` ` `# calculate m (A number ` ` ` `# that has same number ` ` ` `# of digits as n, but ` ` ` `# has 1 in place of non-zero ` ` ` `# digits 0 in place of 0 digits) ` ` ` `temp ` `=` `n; ` ` ` `m ` `=` `0` `; ` ` ` `p ` `=` `1` `; ` ` ` `while` `(temp): ` ` ` `rem ` `=` `temp ` `%` `10` `; ` ` ` `temp ` `=` `int` `(temp ` `/` `10` `); ` ` ` ` ` `if` `(rem !` `=` `0` `): ` ` ` `m ` `+` `=` `p; ` ` ` `p ` `*` `=` `10` `; ` ` ` ` ` `print` `(m,end` `=` `" "` `); ` ` ` ` ` `# subtract m from n ` ` ` `n ` `=` `n ` `-` `m; ` ` ` `# Driver code ` `n ` `=` `31` `; ` `psuedoBinary(n); ` ` ` `# This code is contributed ` `# by mits. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to represent a given ` `// number as sum of minimum possible ` `// psuedobinary numbers ` ` ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `void` `psuedoBinary(` `int` `n) ` ` ` `{ ` ` ` `// Repeat below steps until n > 0 ` ` ` `while` `(n != 0) ` ` ` `{ ` ` ` `// calculate m (A number that has same ` ` ` `// number of digits as n, but has 1 in ` ` ` `// place of non-zero digits 0 in place ` ` ` `// of 0 digits) ` ` ` `int` `temp = n, m = 0, p = 1; ` ` ` `while` `(temp != 0) ` ` ` `{ ` ` ` `int` `rem = temp % 10; ` ` ` `temp = temp / 10; ` ` ` ` ` `if` `(rem != 0) ` ` ` `m += p; ` ` ` ` ` `p *= 10; ` ` ` `} ` ` ` ` ` `Console.Write(m + ` `" "` `); ` ` ` ` ` `// subtract m from n ` ` ` `n = n - m; ` ` ` `} ` ` ` `Console.Write(` `" "` `); ` ` ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 31; ` ` ` `psuedoBinary(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to represent a ` `// given number as sum of minimum ` `// possible psuedobinary numbers ` ` ` `// Function to represent a ` `// given number as sum of minimum ` `// possible psuedobinary numbers ` `function` `psuedoBinary(` `$n` `) ` `{ ` ` ` `// Repeat below steps until n > 0 ` ` ` `while` `(` `$n` `> 0) ` ` ` `{ ` ` ` `// calculate m (A number ` ` ` `// that has same number of ` ` ` `// digits as n, but has 1 ` ` ` `// in place of non-zero ` ` ` `// digits 0 in place of 0 ` ` ` `// digits) ` ` ` `$temp` `= ` `$n` `; ` `$m` `= 0; ` `$p` `= 1; ` ` ` `while` `(` `$temp` `) ` ` ` `{ ` ` ` `$rem` `= ` `$temp` `% 10; ` ` ` `$temp` `= ` `$temp` `/ 10; ` ` ` ` ` `if` `(` `$rem` `!= 0) ` ` ` `$m` `+= ` `$p` `; ` ` ` ` ` `$p` `*= 10; ` ` ` `} ` ` ` ` ` `echo` `$m` `, ` `" "` `; ` ` ` ` ` `// subtract m from n ` ` ` `$n` `= ` `$n` `- ` `$m` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `$n` `= 31; ` `psuedoBinary(` `$n` `); ` ` ` `// This code is contributed ` `// by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

11 10 10

**Time Complexity** : O( log n )

**Auxiliary Space** : O(1)

This article is contributed by **Harsh Agarwal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Minimum number of given powers of 2 required to represent a number
- Represent the fraction of two numbers in the string format
- Minimum number of sets with numbers less than Y
- Represent n as the sum of exactly k powers of two | Set 2
- Represent a given set of points by the best possible straight line
- Minimum numbers (smaller than or equal to N) with sum S
- Minimum sum of two numbers formed from digits of an array
- Minimum numbers needed to express every integer below N as a sum
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Minimum multiplications with {2, 3, 7} to make two numbers equal
- Find minimum number to be divided to make a number a perfect square
- Find the minimum difference between Shifted tables of two numbers
- Pair of prime numbers with a given sum and minimum absolute difference
- Maximum and minimum sums from two numbers with digit replacements
- Convert a number m to n using minimum number of given operations