Maximum number with same digit factorial product in C++

C++Server Side ProgrammingProgramming

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

 Live Demo

#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
raja
Published on 17-Aug-2020 09:26:08
Advertisements