Program to partitioning into minimum number of Deci- Binary numbers in Python

PythonServer Side ProgrammingProgramming

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 −

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
raja
Updated on 06-Oct-2021 11:06:00

Advertisements