

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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++
<p style="">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.</p><p>Let’s now understand what we have to do using an example −</p><p><strong>Input</strong> − N = 4912</p><p><strong>Output</strong> − 73332222</p><p style=""><strong>Explanation</strong> − 4! * 9! * 1! * 2! = 7! * 3! * 3! * 3! * 2! * 2! *2! *2! = 17,418,240</p><p><strong>Input</strong> − N = 340</p><p><strong>Output</strong> − 3322</p><h2>Approach used in the below program as follows</h2><ul class="list"><li><p>In order to attain the maximum answer we will have to express the given number as a product of factorial of prime numbers.</p><p>If the given number contains only zeros and one then finding an output is not possible.</p></li><li><p>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.</p></li><li><p>Loop from i=0 till i<total_digits and check for every digit if it is prime or not.</p></li><li><p>If the current digit is prime, then simply add 1 to that position of the array Frq[].</p></li><li><p>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.</p></li><li><p>Create an empty string ‘ans’ to store the final answer.</p></li><li><p>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.</p></li><li><p>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.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/omCz9aOM" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate" style="">#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; }</pre><h2>Output</h2><p>If we run the above code we will get the following output −</p><pre class="result notranslate">3322</pre>
- Related Questions & Answers
- First digit in factorial of a number in C++
- C++ program to find first digit in factorial of a number
- Tuple with the same Product in C++
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Maximum number of contiguous array elements with same number of set bits in C++
- Product of N with its largest odd digit 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++
- Program to find tuple with same product in Python
- Find maximum power of a number that divides a factorial in C++
- Finding the group with largest elements with same digit sum in JavaScript
- Maximum sum by adding numbers with same number of set bits in C++
- Maximum Product Subarray in Python
- Maximum Product Subarray | Added negative product case in C++
- Factorial of a large number
Advertisements