Find maximum number that can be formed using digits of a given number
Last Updated :
28 May, 2022
Given a number, write a program to find a maximum number that can be formed using all of the digits of this number.
Examples:
Input : 38293367
Output : 98763332
Input : 1203465
Output: 6543210
Simple Approach: The simple method to solve this problem is to extract and store the digits of the given number in an integer array and sort this array in descending order. After sorting the array, print the elements of the array.
Time Complexity: O( N log N ), where N is the number of digits in the given number.
Efficient approach : We know that the digits in a number will range from 0-9, so the idea is to create a hashed array of size 10 and store the count of every digit in the hashed array that occurs in the number. Then traverse the hashed array from index 9 to 0 and calculate the number accordingly.
Below is the implementation of above efficient approach:
C++
Java
public class GFG
{
static int printMaxNum( int num)
{
int count[] = new int [ 10 ];
String str = Integer.toString(num);
for ( int i= 0 ; i < str.length(); i++)
count[str.charAt(i)- '0' ]++;
int result = 0 , multiplier = 1 ;
for ( int i = 0 ; i <= 9 ; i++)
{
while (count[i] > 0 )
{
result = result + (i * multiplier);
count[i]--;
multiplier = multiplier * 10 ;
}
}
return result;
}
public static void main(String[] args)
{
int num = 38293367 ;
System.out.println(printMaxNum(num));
}
}
|
Python3
def printMaximum(inum):
count = [ 0 for x in range ( 10 )]
string = str (num)
for i in range ( len (string)):
count[ int (string[i])] = count[ int (string[i])] + 1
result = 0
multiplier = 1
for i in range ( 10 ):
while count[i] > 0 :
result = result + ( i * multiplier )
count[i] = count[i] - 1
multiplier = multiplier * 10
return result
num = 38293367
print (printMaximum(num))
|
C#
using System;
class GFG
{
static int printMaxNum( int num)
{
int []count = new int [10];
String str = num.ToString();
for ( int i = 0; i < str.Length; i++)
count[str[i] - '0' ]++;
int result = 0, multiplier = 1;
for ( int i = 0; i <= 9; i++)
{
while (count[i] > 0)
{
result = result + (i * multiplier);
count[i]--;
multiplier = multiplier * 10;
}
}
return result;
}
public static void Main()
{
int num = 38293367;
Console.Write(printMaxNum(num));
}
}
|
PHP
<?php
function printMaxNum( $num )
{
$count = array_fill (0,10, NULL);
$str = (string) $num ;
for ( $i =0; $i < strlen ( $str ); $i ++)
$count [ord( $str [ $i ])-ord( '0' )]++;
$result = 0;
$multiplier = 1;
for ( $i = 0; $i <= 9; $i ++)
{
while ( $count [ $i ] > 0)
{
$result = $result + ( $i * $multiplier );
$count [ $i ]--;
$multiplier = $multiplier * 10;
}
}
return $result ;
}
$num = 38293367;
echo printMaxNum( $num );
?>
|
Javascript
<script>
function printMaxNum(num)
{
let count = new Array(10);
for (let i=0;i<count.length;i++)
{
count[i]=0;
}
let str = num.toString();
for (let i=0; i < str.length; i++)
count[str[i]- '0' ]++;
let result = 0, multiplier = 1;
for (let i = 0; i <= 9; i++)
{
while (count[i] > 0)
{
result = result + (i * multiplier);
count[i]--;
multiplier = multiplier * 10;
}
}
return result;
}
let num = 38293367;
document.write(printMaxNum(num));
</script>
|
Output:
98763332
Time Complexity: O( N ), where N is the number of digits in the given number.
Auxiliary Space: O(1)
Note: For very large numbers we can use strings to take input instead of storing input in integer data type.
Share your thoughts in the comments
Please Login to comment...