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++
#include <bits/stdc++.h>
using namespace std;
int printMaxNum(int num)
{
int count[10] = {0};
string str = to_string(num);
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;
}
int main()
{
int num = 38293367;
cout << printMaxNum(num);
return 0;
}
|
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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!