Maximum number with same digit factorial product in C++

C++Server Side ProgrammingProgramming

<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&rsquo;s now understand what we have to do using an example &minus;</p><p><strong>Input</strong> &minus; N = 4912</p><p><strong>Output</strong> &minus; 73332222</p><p style=""><strong>Explanation</strong> &minus; 4! * 9! * 1! * 2! = 7! * 3! * 3! * 3! * 2! * 2! *2! *2! = 17,418,240</p><p><strong>Input</strong> &minus; N = 340</p><p><strong>Output</strong> &minus; 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&lt;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 &lsquo;ans&rsquo; 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&gt;=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">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate" style="">#include &lt;bits/stdc++.h&gt; using namespace std; string MaxNum(string str){ &nbsp; &nbsp;int total_digits = str.length(); &nbsp; &nbsp;int Frq[15] = { 0 }; &nbsp; &nbsp;//Obtaining the frequency of every digit &nbsp; &nbsp;for (int i = 0; i &lt; total_digits; i++){ &nbsp; &nbsp; &nbsp; if (str[i] == &#39;1&#39;|| str[i] == &#39;2&#39;|| str[i] == &#39;3&#39;|| str[i] == &#39;5&#39;|| str[i] == &#39;7&#39;){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[str[i] - 48] += 1; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; // 4! = 2! * 2! * 3! &nbsp; &nbsp; &nbsp; if (str[i] == &#39;4&#39;){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[2] += 2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[3]++; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; // 6! = 5! * 3! &nbsp; &nbsp; &nbsp; if (str[i] == &#39;6&#39;){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[5]++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[3]++; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; // 8! = 7! * 2! * 2! * 2! &nbsp; &nbsp; &nbsp; if (str[i] == &#39;8&#39;){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[7]++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[2] += 3; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; // 9! = 7! * 3! * 3! * 2! &nbsp; &nbsp; &nbsp; if (str[i] == &#39;9&#39;){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[7]++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[3] += 2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Frq[2]++; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;string ans = &quot;&quot;; &nbsp; &nbsp;//If number has only 1 or 0 &nbsp; &nbsp;if (Frq[1] == total_digits || Frq[0] == total_digits || (Frq[0] + Frq[1]) == total_digits){ &nbsp; &nbsp; &nbsp; return str; &nbsp; &nbsp;} &nbsp; &nbsp;else{ &nbsp; &nbsp; &nbsp; //Maximum number possible &nbsp; &nbsp; &nbsp; for (int i = 9; i &gt;= 2; i--){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int C = Frq[i]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (C--){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ans += (char)(i + 48); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; return ans; &nbsp; &nbsp;} } //Main function int main(){ &nbsp; &nbsp;string str = &quot;340&quot;; &nbsp; &nbsp;cout &lt;&lt; MaxNum(str); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code we will get the following output &minus;</p><pre class="result notranslate">3322</pre>
raja
Updated on 17-Aug-2020 09:26:44

Advertisements