Partitioning into Minimum Number of Deci-Binary Numbers in Python



Suppose we have a number n in string format. We have to find minimum deci-binary numbers are required, so that whose sum is equal to n. A deci-binary number is a decimal number whose digits are either 0 or 1.

So, if the input is like n = "132", then the output will be 3 because 132 is sum of three decibinary number (10 + 11 + 111).

To solve this, we will follow these steps −

  • result := 1
  • for each i in n, do
    • if i is not in {0,1}, then
      • result := maximum of result and i
  • return result

Example

Let us see the following implementation to get better understanding −

Open Compiler
def solve(n): result = 1 for i in n: if i not in {0,1}: result = max(result, int(i)) return result n = "132" print(solve(n))

Input

132

Output

3
Updated on: 2021-10-06T11:06:00+05:30

498 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements