Minimum Factorization in C++

Suppose we have a positive integer x, we have to find the smallest positive integer b whose multiplication of each digit equals to x. If we have no such answer then return 0.

So, if the input is like 48, then the output will be 68

To solve this, we will follow these steps −

  • ret := 0, mul := 1

  • if a

    • return a

  • for initialize i := 9, when i >= 2, update (decrease i by 1), do −

    • while a mod i is same as 0, do −

      • ret := i * mul + ret

      • mul := mul * 10

      • a := a / i

  • return (if a

Example

Let us see the following implementation to get better understanding −

 Live Demo

#include 
using namespace std;
typedef long long int lli;
class Solution {
public:
   int smallestFactorization(int a) {
      lli ret = 0;
      lli mul = 1;
      if (a = 2; i--) {
         while (a % i == 0) {
            ret = i * mul + ret;
            mul *= 10;
            a /= i;
         }
      }
      return a 

Input

48

Output

68
Updated on: 2020-11-16T14:26:26+05:30

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements