- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum number with same digit factorial product in C++
Given the task is to find the maximum number without any leading or trailing zeroes or ones whose product of factorial of its digits is equal to the product of factorial of digits of the given number N.
Let’s now understand what we have to do using an example −
Input − N = 4912
Output − 73332222
Explanation − 4! * 9! * 1! * 2! = 7! * 3! * 3! * 3! * 2! * 2! *2! *2! = 17,418,240
Input − N = 340
Output − 3322
Approach used in the below program as follows
In order to attain the maximum answer we will have to express the given number as a product of factorial of prime numbers.
If the given number contains only zeros and one then finding an output is not possible.
In function MaxNum() create a variable total_digits of type int to store the total number of digits and also initialize another array Frq[] = {0} of type int to store the frequencies of each occurring number.
Loop from i=0 till i<total_digits and check for every digit if it is prime or not.
If the current digit is prime, then simply add 1 to that position of the array Frq[].
Else of the digit is not prime, then through separate if statements check if it is either 4, 6, 8 or 9 and then break it down into its basic prime factorials and increment the frequency accordingly.
Create an empty string ‘ans’ to store the final answer.
Before proceeding to the final step, check if the number contains only ones and zeroes. If so then simply return the original string otherwise proceed to next step.
Loop from i=9 till i>=2. Initialize a variable C = Frq[i] of type int and inside the for loop create a while loop with condition while(C--) in which put ans+=(char)(i+48) to store the final answer into the string ans.
Example
#include <bits/stdc++.h> using namespace std; string MaxNum(string str){ int total_digits = str.length(); int Frq[15] = { 0 }; //Obtaining the frequency of every digit for (int i = 0; i < total_digits; i++){ if (str[i] == '1'|| str[i] == '2'|| str[i] == '3'|| str[i] == '5'|| str[i] == '7'){ Frq[str[i] - 48] += 1; } // 4! = 2! * 2! * 3! if (str[i] == '4'){ Frq[2] += 2; Frq[3]++; } // 6! = 5! * 3! if (str[i] == '6'){ Frq[5]++; Frq[3]++; } // 8! = 7! * 2! * 2! * 2! if (str[i] == '8'){ Frq[7]++; Frq[2] += 3; } // 9! = 7! * 3! * 3! * 2! if (str[i] == '9'){ Frq[7]++; Frq[3] += 2; Frq[2]++; } } string ans = ""; //If number has only 1 or 0 if (Frq[1] == total_digits || Frq[0] == total_digits || (Frq[0] + Frq[1]) == total_digits){ return str; } else{ //Maximum number possible for (int i = 9; i >= 2; i--){ int C = Frq[i]; while (C--){ ans += (char)(i + 48); } } return ans; } } //Main function int main(){ string str = "340"; cout << MaxNum(str); return 0; }
Output
If we run the above code we will get the following output −
3322
- Related Articles
- First digit in factorial of a number in C++
- C++ program to find first digit in factorial of a number
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Find maximum power of a number that divides a factorial in C++
- Tuple with the same Product in C++
- Maximum number of contiguous array elements with same number of set bits in C++
- Find the last digit when factorial of A divides factorial of B in C++
- Find the Number of Maximum Product Quadruples in C++
- Product of N with its largest odd digit in C
- Maximum sum by adding numbers with same number of set bits in C++
- Maximum GCD of N integers with given product in C++
- Maximum length subarray with LCM equal to product in C++
- Maximum Product Subarray | Added negative product case in C++
- Maximum and minimum sums from two numbers with digit replacements in C++
- Find a pair with maximum product in array of Integers in C++
