Digit multiplier | Practice | GeeksforGeeks

Given a positive integer N, find the smallest number S such that the product of all the digits of S is equal to the number N. If there's no such number, Print "-1".
 
Example 1:
Input:
N = 100
Output:
455
Explanation:
4*5*5 = 100. There are no numbers
less than 455 with the product of
it's digit as 100.
Example 2:
Input:
N = 26
Output:
-1
Explanation:
There are no numbers which can form
a product of 26 with its digits.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function getSmallest() which takes an Integer N as input and returns the answer as a String.
 
Expected Time Complexity: O(log(N))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 109
0
luka191miku2021 month ago
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;


ll PRODIG(ll n)
{
    ll k=0;
    ll ans=0;
    for (ll i=9;i>=2 && n>1;i--)
    {
        if (n%i==0)
        {
            while (n%i==0)
            {
                k=k*10+i;
                n=n/i;
            }
        }
    }
    if (n!=1)
    {
        return -1;
    }
    while (k>0)
    {
        ll a=k%10;
        ans=ans*10+a;
        k=k/10;
    }
    return ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    freopen("PRODIG.INP","r",stdin);
    freopen("PRODIG.OUT","w",stdout);
    ll n;
    cin>>n;
    cout <<PRODIG(n);
    return 0;
}
0
luka191miku202
This comment was deleted.
0
taiphanvan24032 months ago
string getSmallest(long long N) {
        int tam=0;
        if(N<=9) return to_string(N);
        for(int i=9;i>=2;i--){
            while(N>0 && (N%i==0)){
                tam=tam*10+i;
                N/=i;
            }
        }
        if(N!=1) return "-1";
        string res=to_string(tam);
        reverse(res.begin(),res.end());
        return res;
    }
0
ankitparashxr3 months ago
java
if(n>=0 && n<=9)       {           return n+"";       }       Stack<Integer> st = new Stack<>();       String s = "";              for(int i=9;i>=2 && n>1;i--)       {           while(n%i==0)           {               st.push(i);               s= i+s;               n/=i;           }       }       if(n!=1)       {         return "-1";       }                     return s;
0
utkarshrdce4 months ago
wierd dp solution, but okay
 
#define ll long long unordered_map<ll, ll> dp;  ll min(ll a, ll b) {return a < b ? a : b;}   bool isPrime(ll  N) {       for(int i = 2; i*i <= N; i++) {           if(N%i == 0) return false;       }       return true;   }   ll f(long long N) {             if(N % 10 == N) return N;      if(isPrime(N)) return LONG_MAX;            if(dp.count(N)) return dp[N];             ll ans = LONG_MAX;      for(ll i = 2; i <= min(sqrt(N), 9); i++) {                    if(N % i == 0) {              ll curAns1 = f(N / i);                                          if(curAns1 != LONG_MAX) ans = min(ans, curAns1 * 10 + i);              if(N / i < 10) {                   ll curAns2 = f(i);                   if(curAns2 != LONG_MAX) ans = min(ans, curAns2 * 10 + (N / i));                                 }          }      }      return dp[N] = ans;   }   string getSmallest(long long N) {       // code here       if(N % 10 == N) return to_string(N);       if(isPrime(N)) return "-1";              // return to_string(f(N));       ll ans = f(N);       return ans == LONG_MAX ? "-1" : to_string(ans);                        }
0
chessnoobdj4 months ago
Easy c++
string getSmallest(long long N) {
        if(N == 1)
            return "1";
        bool flg = true;
        string s = "";
        while(flg){
            for(int i=9; i>=2; i--){
                flg = false;
                if(N%i == 0){
                    s += (i+'0');
                    N /= i;
                    flg = true;
                    break;
                }
            }
        }
        reverse(s.begin(), s.end());
        return N==1 ? s : "-1";
    }
0
Dhanyaa Bharadwaj9 months ago
Dhanyaa Bharadwaj
C++ driver code needs correction. Please look into it.
0
Dhanyaa Bharadwaj
This comment was deleted.
0
shahzdor10 months ago
shahzdor
Python solution:
import numpy as np
class Solution:    def getSmallest(self, N):      if N<=10:        return N      else:        list1=[]        number=N        first=9        while number>1 and first>1:          if number%first==0:            list1.append(first)            number=number/first          else:            first=first-1      if np.prod(list1)==N:        return ''.join(map(str,list1[::-1]))      else:        return '-1'
0
abccba11 months ago
abccba
if(n==1)return "1";        string ans="";
        for(long long  i=9;i>=2;i--)        {            while((n%i==0)&&n>=i)            {                ans+=to_string(i);                n=n/i;               // cout<<i; }="" }="" cout<<ans;="" if(n="">=10)return "-1";        if(n>1)ans+=to_string(n);        reverse(ans.begin(),ans.end());        return ans;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.