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.
Examples:
Input: N = 4370
Output: 73322
4! * 3! * 7! * 0! = 7! * 3! * 3! * 2! * 2! = 725760
Input: N = 1280
Output: 72222
1! * 2! * 8! * 0! = 7! * 2! * 2! * 2! * 2! = 80640
Approach:
- 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.
Illustration:
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 :
Hence The output is 73322.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string getNumber(string s)
{
int number_of_digits = s.length();
int freq[10] = { 0 };
for ( int i = 0; i < number_of_digits; i++) {
if (s[i] == '1'
|| s[i] == '2'
|| s[i] == '3'
|| s[i] == '5'
|| s[i] == '7' ) {
freq[s[i] - 48] += 1;
}
if (s[i] == '4' ) {
freq[2] += 2;
freq[3]++;
}
if (s[i] == '6' ) {
freq[5]++;
freq[3]++;
}
if (s[i] == '8' ) {
freq[7]++;
freq[2] += 3;
}
if (s[i] == '9' ) {
freq[7]++;
freq[3] += 2;
freq[2]++;
}
}
string t = "" ;
if (freq[1] == number_of_digits
|| freq[0] == number_of_digits
|| (freq[0] + freq[1]) == number_of_digits) {
return s;
}
else {
for ( int i = 9; i >= 2; i--) {
int ctr = freq[i];
while (ctr--) {
t += ( char )(i + 48);
}
}
return t;
}
}
int main()
{
string s = "1280" ;
cout << getNumber(s);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String getNumber(String s)
{
int number_of_digits = s.length();
int freq[] = new int [ 10 ];
for ( int i = 0 ; i < number_of_digits; i++) {
if (s.charAt(i) == '1'
|| s.charAt(i) == '2'
|| s.charAt(i) == '3'
|| s.charAt(i) == '5'
|| s.charAt(i) == '7' ) {
freq[s.charAt(i) - 48 ] += 1 ;
}
if (s.charAt(i) == '4' ) {
freq[ 2 ] += 2 ;
freq[ 3 ]++;
}
if (s.charAt(i) == '6' ) {
freq[ 5 ]++;
freq[ 3 ]++;
}
if (s.charAt(i) == '8' ) {
freq[ 7 ]++;
freq[ 2 ] += 3 ;
}
if (s.charAt(i) == '9' ) {
freq[ 7 ]++;
freq[ 3 ] += 2 ;
freq[ 2 ]++;
}
}
String t = "" ;
if (freq[ 1 ] == number_of_digits
|| freq[ 0 ] == number_of_digits
|| (freq[ 0 ] + freq[ 1 ]) == number_of_digits) {
return s;
}
else {
for ( int i = 9 ; i >= 2 ; i--) {
int ctr = freq[i];
while ((ctr--)> 0 ) {
t += ( char )(i + 48 );
}
}
return t;
}
}
public static void main (String[] args) {
String s = "1280" ;
System.out.println(getNumber(s));
}
}
|
Python3
def getNumber(s):
number_of_digits = len (s);
freq = [ 0 ] * 10 ;
for i in range (number_of_digits):
if (s[i] = = '1' or s[i] = = '2' or s[i] = = '3' or s[i] = = '5' or s[i] = = '7' ):
freq[ ord (s[i]) - 48 ] + = 1 ;
if (s[i] = = '4' ):
freq[ 2 ] + = 2 ;
freq[ 3 ] + = 1 ;
if (s[i] = = '6' ):
freq[ 5 ] + = 1 ;
freq[ 3 ] + = 1 ;
if (s[i] = = '8' ):
freq[ 7 ] + = 1 ;
freq[ 2 ] + = 3 ;
if (s[i] = = '9' ):
freq[ 7 ] + = 1 ;
freq[ 3 ] + = 2 ;
freq[ 2 ] + = 1 ;
t = "";
if (freq[ 1 ] = = number_of_digits or freq[ 0 ] = = number_of_digits or (freq[ 0 ] + freq[ 1 ]) = = number_of_digits):
return s;
else :
for i in range ( 9 , 1 , - 1 ):
ctr = freq[i];
while (ctr> 0 ):
t + = chr (i + 48 );
ctr - = 1 ;
return t;
s = "1280" ;
print (getNumber(s));
|
C#
using System;
class GFG
{
static String getNumber( string s)
{
int number_of_digits = s.Length;
int []freq = new int [10];
for ( int i = 0;
i < number_of_digits; i++)
{
if (s[i] == '1' || s[i] == '2' ||
s[i] == '3' || s[i] == '5' ||
s[i] == '7' )
{
freq[s[i] - 48] += 1;
}
if (s[i] == '4' )
{
freq[2] += 2;
freq[3]++;
}
if (s[i] == '6' )
{
freq[5]++;
freq[3]++;
}
if (s[i] == '8' )
{
freq[7]++;
freq[2] += 3;
}
if (s[i] == '9' )
{
freq[7]++;
freq[3] += 2;
freq[2]++;
}
}
string t = "" ;
if (freq[1] == number_of_digits ||
freq[0] == number_of_digits ||
(freq[0] + freq[1]) == number_of_digits)
{
return s;
}
else
{
for ( int i = 9; i >= 2; i--)
{
int ctr = freq[i];
while ((ctr--)>0)
{
t += ( char )(i + 48);
}
}
return t;
}
}
public static void Main ()
{
string s = "1280" ;
Console.WriteLine(getNumber(s));
}
}
|
PHP
<?php
function getNumber( $s )
{
$number_of_digits = strlen ( $s );
$freq = array_fill (0,10,0);
for ( $i = 0; $i < $number_of_digits ; $i ++) {
if ( $s [ $i ] == '1'
|| $s [ $i ] == '2'
|| $s [ $i ] == '3'
|| $s [ $i ] == '5'
|| $s [ $i ] == '7' ) {
$freq [ord( $s [ $i ]) - 48] += 1;
}
if ( $s [ $i ] == '4' ) {
$freq [2] += 2;
$freq [3]++;
}
if ( $s [ $i ] == '6' ) {
$freq [5]++;
$freq [3]++;
}
if ( $s [ $i ] == '8' ) {
$freq [7]++;
$freq [2] += 3;
}
if ( $s [ $i ] == '9' ) {
$freq [7]++;
$freq [3] += 2;
$freq [2]++;
}
}
$t = "" ;
if ( $freq [1] == $number_of_digits
|| $freq [0] == $number_of_digits
|| ( $freq [0] + $freq [1]) == $number_of_digits ) {
return $s ;
}
else {
for ( $i = 9; $i >= 2; $i --) {
$ctr = $freq [ $i ];
while ( $ctr --) {
$t .= chr ( $i + 48);
}
}
return $t ;
}
}
$s = "1280" ;
echo getNumber( $s );
?>
|
Javascript
<script>
function getNumber(s)
{
let number_of_digits = s.length;
let freq = new Array(10);
freq.fill(0);
for (let i = 0; i < number_of_digits; i++)
{
if (s[i] == '1' || s[i] == '2' ||
s[i] == '3' || s[i] == '5' ||
s[i] == '7' )
{
freq[s[i].charCodeAt() - 48] += 1;
}
if (s[i] == '4' )
{
freq[2] += 2;
freq[3]++;
}
if (s[i] == '6' )
{
freq[5]++;
freq[3]++;
}
if (s[i] == '8' )
{
freq[7]++;
freq[2] += 3;
}
if (s[i] == '9' )
{
freq[7]++;
freq[3] += 2;
freq[2]++;
}
}
let t = "" ;
if (freq[1] == number_of_digits ||
freq[0] == number_of_digits ||
(freq[0] + freq[1]) == number_of_digits)
{
return s;
}
else
{
for (let i = 9; i >= 2; i--)
{
let ctr = freq[i];
while ((ctr--)>0)
{
t += String.fromCharCode(i + 48);
}
}
return t;
}
}
let s = "1280" ;
document.write(getNumber(s));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Jul, 2022
Like Article
Save Article