Smallest number k such that the product of digits of k is equal to n

20 May, 2021
Given a non-negative number n. The problem is to find the smallest number k such that the product of digits of k is equal to n. If no such number k can be formed then print “-1”.Examples:  
Input : 100
Output : 455
4*5*5 = 100 and 455 is the
smallest possible number.

Input : 26
Output : -1
Source: Asked in Amazon Interview 
Approach: For each i = 9 to 2, repeatedly divide n by i until it cannot be further divided or the list of numbers from 9 to 2 gets finished. Also, in the process of division push each digit i onto the stack which divides n completely. After the above process gets completed check whether n == 1 or not. If not, then print “-1”, else form the number k using the digits from the stack containing the digits in the same sequence as popped from the stack.  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to find smallest number k such that// the product of digits of k is equal to n#include <bits/stdc++.h> using namespace std; // function to find smallest number k such that// the product of digits of k is equal to nlong long int smallestNumber(int n){    // if 'n' is a single digit number, then    // it is the required number    if (n >= 0 && n <= 9)        return n;         // stack the store the digits    stack<int> digits;         // repeatedly divide 'n' by the numbers    // from 9 to 2 until all the numbers are    // used or 'n' > 1    for (int i=9; i>=2 && n > 1; i--)    {        while (n % i == 0)        {            // save the digit 'i' that divides 'n'            // onto the stack            digits.push(i);            n = n / i;        }    }         // if true, then no number 'k' can be formed    if (n != 1)        return -1;     // pop digits from the stack 'digits'    // and add them to 'k'    long long int k = 0;    while (!digits.empty())    {        k = k*10 + digits.top();        digits.pop();    }         // required smallest number    return k;} // Driver program to test aboveint main(){    int n = 100;    cout << smallestNumber(n);    return 0;}
//Java implementation to find smallest number k such that// the product of digits of k is equal to nimport java.util.Stack; public class GFG { // function to find smallest number k such that// the product of digits of k is equal to n    static long smallestNumber(int n) {        // if 'n' is a single digit number, then        // it is the required number        if (n >= 0 && n <= 9) {            return n;        }         // stack the store the digits        Stack<Integer> digits = new Stack<>();         // repeatedly divide 'n' by the numbers        // from 9 to 2 until all the numbers are        // used or 'n' > 1        for (int i = 9; i >= 2 && n > 1; i--) {            while (n % i == 0) {                // save the digit 'i' that divides 'n'                // onto the stack                digits.push(i);                n = n / i;            }        }         // if true, then no number 'k' can be formed        if (n != 1) {            return -1;        }         // pop digits from the stack 'digits'        // and add them to 'k'        long k = 0;        while (!digits.empty()) {            k = k * 10 + digits.peek();            digits.pop();        }         // required smallest number        return k;    } // Driver program to test above    static public void main(String[] args) {        int n = 100;        System.out.println(smallestNumber(n));    }} /*This code is contributed by PrinciRaj1992*/
# Python3 implementation to find smallest# number k such that the product of digits# of k is equal to nimport math as mt # function to find smallest number k such that# the product of digits of k is equal to ndef smallestNumber(n):     # if 'n' is a single digit number, then    # it is the required number    if (n >= 0 and n <= 9):        return n         # stack the store the digits    digits = list()         # repeatedly divide 'n' by the numbers    # from 9 to 2 until all the numbers are    # used or 'n' > 1    for i in range(9,1, -1):             while (n % i == 0):                     # save the digit 'i' that            # divides 'n' onto the stack            digits.append(i)            n = n //i             # if true, then no number 'k'    # can be formed    if (n != 1):        return -1     # pop digits from the stack 'digits'    # and add them to 'k'    k = 0    while (len(digits) != 0):             k = k * 10 + digits[-1]        digits.pop()         # required smallest number    return k # Driver Coden = 100print(smallestNumber(n)) # This code is contributed by# Mohit kumar 29
     // C# implementation to find smallest number k such that// the product of digits of k is equal to nusing System;using System.Collections.Generic;public class GFG {  // function to find smallest number k such that// the product of digits of k is equal to n    static long smallestNumber(int n) {        // if 'n' is a single digit number, then        // it is the required number        if (n >= 0 && n <= 9) {            return n;        }          // stack the store the digits        Stack<int> digits = new Stack<int>();          // repeatedly divide 'n' by the numbers        // from 9 to 2 until all the numbers are        // used or 'n' > 1        for (int i = 9; i >= 2 && n > 1; i--) {            while (n % i == 0) {                // save the digit 'i' that divides 'n'                // onto the stack                digits.Push(i);                n = n / i;            }        }          // if true, then no number 'k' can be formed        if (n != 1) {            return -1;        }          // pop digits from the stack 'digits'        // and add them to 'k'        long k = 0;        while (digits.Count!=0) {            k = k * 10 + digits.Peek();            digits.Pop();        }          // required smallest number        return k;    }  // Driver program to test above    static public void Main() {        int n = 100;        Console.Write(smallestNumber(n));    }}  /*This code is contributed by Rajput-Ji*/
<?php// PHP implementation to find smallest number k such that// the product of digits of k is equal to n // function to find smallest number k such that// the product of digits of k is equal to nfunction smallestNumber($n){    // if 'n' is a single digit number, then    // it is the required number    if ($n >= 0 && $n <= 9)        return $n;         // stack the store the digits    $digits = array();         // repeatedly divide 'n' by the numbers    // from 9 to 2 until all the numbers are    // used or 'n' > 1    for ($i = 9; $i >= 2 && $n > 1; $i--)    {        while ($n % $i == 0)        {            // save the digit 'i' that divides 'n'            // onto the stack            array_push($digits,$i);            $n =(int)( $n / $i);        }    }         // if true, then no number 'k' can be formed    if ($n != 1)        return -1;     // pop digits from the stack 'digits'    // and add them to 'k'    $k = 0;    while (!empty($digits))        $k = $k * 10 + array_pop($digits);         // required smallest number    return $k;}     // Driver code    $n = 100;    echo smallestNumber($n); // This code is contributed by mits?>
<script> // Javascript implementation to find// smallest number k such that// the product of digits of k is equal to n         // function to find smallest number k such that// the product of digits of k is equal to n    function smallestNumber(n)    {        // if 'n' is a single digit number, then        // it is the required number        if (n >= 0 && n <= 9) {            return n;        }           // stack the store the digits        let digits = [];           // repeatedly divide 'n' by the numbers        // from 9 to 2 until all the numbers are        // used or 'n' > 1        for (let i = 9; i >= 2 && n > 1; i--) {            while (n % i == 0) {                // save the digit 'i' that divides 'n'                // onto the stack                digits.push(i);                n = Math.floor(n / i);            }        }           // if true, then no number 'k' can be formed        if (n != 1) {            return -1;        }           // pop digits from the stack 'digits'        // and add them to 'k'        let k = 0;        while (digits.length!=0) {            k = k * 10 + digits[digits.length-1];            digits.pop();        }           // required smallest number        return k;    }         // Driver program to test above    let n = 100;    document.write(smallestNumber(n));      // This code is contributed by patel2127 </script>
Output:  
455
Time Complexity: O(num), where num is the size of the stack. Space Complexity: O(num), where num is the size of the stack.We can store the required number k in string for large numbers.This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
princiraj1992
Rajput-Ji
mohit kumar 29
gfg_sal_gfg
Mithun Kumar
aditya1050
nidhi_biet
patel2127
Amazon
number-digits
Numbers
Mathematical
Amazon
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Program for Decimal to Binary Conversion
Modulo 10^9+7 (1000000007)