Given a string str which represents an integer, the task is to find the largest number without any leading or trailing zeros or ones whose product of the factorial of its digits is equal to the product of the factorial of digits of str.
Input: N = 4370
4! * 3! * 7! * 0! = 7! * 3! * 3! * 2! * 2! = 725760
Input: N = 1280
1! * 2! * 8! * 0! = 7! * 2! * 2! * 2! * 2! = 80640
- Express the factorial of each of the digits of str as product of factorial of prime numbers.
- If str contains only 0 or 1 as its digits, then display the given number as output is not possible without leading and trailing zeros or ones.
- If digits 1, 2, 3, 5 or 7 are encountered then they need to be included as the digits in the resultant number.
- If digits 4, 6, 8 or 9 are encountered then express them as product of factorial of prime numbers,
- 4! can be expressed as 3! * 2! * 2!.
- 6! can be expressed as 5! * 3!.
- 8! can be expressed as 7! * 2! * 2! * 2!.
- And 9! can be expressed as 7! * 3! * 3! * 2!.
- Finally, form the number by arranging the generated digits in descending order in order to get the maximum number satisfying the condition.
Let us consider a given input 4370. The factorial of each of its digits are as follows :
4! = 24 = 2! * 2 ! * 3!
3! = 6 = 3!
7! = 5040 = 7!
Hence the frequency of the digits in the maximum number are :
Frequency of 7 is 1. Frequency of 3 is 2. Frequency of 2 is 2.
Hence The output is 73322.
Below is the implementation of the above approach:
- Maximum of sum and product of digits until number is reduced to a single digit
- First digit in factorial of a number
- Find the last digit when factorial of A divides factorial of B
- Find maximum power of a number that divides a factorial
- Check whether a number can be expressed as a product of single digit numbers
- Last non-zero digit of a factorial
- Find the Number of Maximum Product Quadruples
- Find the number in a range having maximum product of the digits
- Product of N with its largest odd digit
- Digit - Product - Sequence
- First digit in product of an array of numbers
- Count of Numbers in Range where first digit is equal to last digit of the number
- Largest palindrome which is product of two n-digit numbers
- Check if all sub-numbers have distinct Digit product
- Last digit of Product of two Large or Small numbers (a * b)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.