Find maximum number that can be formed using digits of a given number
Last Updated :
27 Feb, 2025
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++
// CPP program to print the maximum number
// from the set of digits of a given number
#include <bits/stdc++.h>
using namespace std;
// Function to print the maximum number
int printMaxNum(int num)
{
// hashed array to store count of digits
int count[10] = {0};
// Converting given number to string
string str = to_string(num);
// Updating the count array
for (int i=0; i<str.length(); i++)
count[str[i]-'0']++;
// result is to store the final number
int result = 0, multiplier = 1;
// Traversing the count array
// to calculate the maximum number
for (int i = 0; i <= 9; i++)
{
while (count[i] > 0)
{
result = result + (i * multiplier);
count[i]--;
multiplier = multiplier * 10;
}
}
// return the result
return result;
}
// Driver program to test above function
int main()
{
int num = 38293367;
cout << printMaxNum(num);
return 0;
}
Java
// Java program to print the maximum number
// from the set of digits of a given number
public class GFG
{
// Function to print the maximum number
static int printMaxNum(int num)
{
// hashed array to store count of digits
int count[] = new int[10];
// Converting given number to string
String str = Integer.toString(num);
// Updating the count array
for(int i=0; i < str.length(); i++)
count[str.charAt(i)-'0']++;
// result is to store the final number
int result = 0, multiplier = 1;
// Traversing the count array
// to calculate the maximum number
for (int i = 0; i <= 9; i++)
{
while (count[i] > 0)
{
result = result + (i * multiplier);
count[i]--;
multiplier = multiplier * 10;
}
}
// return the result
return result;
}
// Driver program to test above function
public static void main(String[] args)
{
int num = 38293367;
System.out.println(printMaxNum(num));
}
}
// This code is contributed by Sumit Ghosh
Python
# Python program to print the maximum number
# from the set of digits of a given number
# Function to print maximum number
def printMaximum(inum):
# Hashed array to store count of digits
count = [0 for x in range(10)]
# Converting given number to string
string = str(num)
# Updating the count array
for i in range(len(string)):
count[int(string[i])] = count[int(string[i])] + 1
# Result stores final number
result = 0
multiplier = 1
# traversing the count array
# to calculate the maximum number
for i in range(10):
while count[i] > 0:
result = result + ( i * multiplier )
count[i] = count[i] - 1
multiplier = multiplier * 10
# return the result
return result
# Driver code
num = 38293367
print(printMaximum(num))
# This code is contributed by Harshit Agrawal
C#
// C# program to print the maximum number
// from the set of digits of a given number
using System;
class GFG
{
// Function to print the maximum number
static int printMaxNum(int num)
{
// hashed array to store
// count of digits
int []count = new int[10];
// Converting given number
// to string
String str = num.ToString();
// Updating the count array
for(int i = 0; i < str.Length; i++)
count[str[i] - '0']++;
// result is to store the
// final number
int result = 0, multiplier = 1;
// Traversing the count array
// to calculate the maximum number
for (int i = 0; i <= 9; i++)
{
while (count[i] > 0)
{
result = result + (i * multiplier);
count[i]--;
multiplier = multiplier * 10;
}
}
// return the result
return result;
}
// Driver Code
public static void Main()
{
int num = 38293367;
Console.Write(printMaxNum(num));
}
}
// This code is contributed
// by PrinciRaj1992
JavaScript
<script>
// Javascript program to print the maximum number
// from the set of digits of a given number
// Function to print the maximum number
function printMaxNum(num)
{
// hashed array to store count of digits
let count = new Array(10);
for(let i=0;i<count.length;i++)
{
count[i]=0;
}
// Converting given number to string
let str = num.toString();
// Updating the count array
for(let i=0; i < str.length; i++)
count[str[i]-'0']++;
// result is to store the final number
let result = 0, multiplier = 1;
// Traversing the count array
// to calculate the maximum number
for (let i = 0; i <= 9; i++)
{
while (count[i] > 0)
{
result = result + (i * multiplier);
count[i]--;
multiplier = multiplier * 10;
}
}
// return the result
return result;
}
// Driver program to test above function
let num = 38293367;
document.write(printMaxNum(num));
//This code is contributed by avanitrachhadiya2155
</script>
PHP
<?php
// Php program to print the maximum number
// from the set of digits of a given number
// Function to print the maximum number
function printMaxNum($num)
{
// hashed array to store count of digits
$count = array_fill(0,10, NULL);
// Converting given number to string
$str = (string)$num;
// Updating the count array
for ($i=0; $i<strlen($str); $i++)
$count[ord($str[$i])-ord('0')]++;
// result is to store the final number
$result = 0;
$multiplier = 1;
// Traversing the count array
// to calculate the maximum number
for ($i = 0; $i <= 9; $i++)
{
while ($count[$i] > 0)
{
$result = $result + ($i * $multiplier);
$count[$i]--;
$multiplier = $multiplier * 10;
}
}
// return the result
return $result;
}
// Driver program to test above function
$num = 38293367;
echo printMaxNum($num);
?>
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.
Similar Reads
Find count of digits in a number that divide the number Given a positive integer n. The task is to find count of digits of number which evenly divides the number n.Examples: Input : n = 12 Output : 2 1 and 2 divide 12. Input : n = 1012 Output : 3 1, 1 and 2 divide 1012. Recommended PracticeCount DigitsTry It! The idea is to find each digit of the number
4 min read
Number of n digit numbers that do not contain 9 Given a number n, find how many n digit number can be formed that does not contain 9 as it's digit.Examples: Input : 1 Output : 8 Explanation : Except 9, all numbers are possible Input : 2 Output : 72 Explanation : Except numbers from 90 - 99 and all two digit numbers that does not end with 9 are po
3 min read
Number of digits in the nth number made of given four digits Given an integer n, find the number of digits in the n-th number formed using only the digits 1, 4, 6, and 9, when the numbers are arranged in ascending order.1, 4, 6, 9, 11, 14, 16, 19, 41, 44, 46, 49, 61, 64, 66, 69, 91, 94, 96, 99, 111, 114, 116, 119, ....Examples: Input: n = 6Output: 2Explanatio
10 min read
Find the smallest number whose digits multiply to a given number n Given a number 'n', find the smallest number 'p' such that if we multiply all digits of 'p', we get 'n'. The result 'p' should have minimum two digits.Examples: Input: n = 36 Output: p = 49 // Note that 4*9 = 36 and 49 is the smallest such number Input: n = 100 Output: p = 455 // Note that 4*5*5 = 1
8 min read
Find the Largest number with given number of digits and sum of digits Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max v
13 min read
Sum of all numbers that can be formed with permutations of n digits Given n distinct digits (from 0 to 9), find sum of all n digit numbers that can be formed using these digits. It is assumed that numbers formed with leading 0 are allowed. Example: Input: 1 2 3 Output: 1332 Explanation Numbers Formed: 123 , 132 , 312 , 213, 231 , 321 123 + 132 + 312 + 213 + 231 + 32
8 min read