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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum of sum and product of digits until number is reduced to a single digit
- Last digit of a number raised to last digit of N factorial
- 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
- Number formed by adding product of its max and min digit K times
- Check whether a number can be expressed as a product of single digit numbers
- Last non-zero digit of a factorial
- Find last digit in factorial
- Find the Number of Maximum Product Quadruples
- Number less than equals to N with maximum product of prime factors
- Find the number in a range having maximum product of the digits
- Maximum value of an integer for which factorial can be calculated on a machine
- Product of N with its largest odd digit
- Digit - Product - Sequence
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count of Numbers in Range where first digit is equal to last digit of the number
- First digit in product of an array of numbers
- Find the remainder when First digit of a number is divided by its Last digit
- Largest palindrome which is product of two n-digit numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.