Arrange given Numbers to form Maximum Number
In this problem, we are given a list of numbers from user via console input. We have to arrange these numbers such that the resulting number is the maximum number possible.
The solution is to sort the given list of numbers in decreasing order based on the first digit of these numbers, and then join them.
Program
In the following program, we read a list of numbers from user separated by space. Since the user provides them as a string, we can just split the string into chunks of numbers and sort them as strings lexicographically in descending order. After that, we join them and print the result. We may also convert this into an integer, if required.
Python Program
# Read input
n = input()
# Split input for individual numbers
n_list = n.split(' ')
# Sort in descending order lexicographically
n_list.sort(reverse=True)
# Join the numbers
maximum = ''.join(n_list)
# Convert to integer
maximum = int(maximum)
# Print result
print(maximum)
CopyOutput #1
8 62 1 47
862471
Output #2
7 86 0 14 2563 89 1 9
98986725631410
Reference tutorials for the above program
Summary
In this Python Tutorial, we learned how to re-arrange given numbers to form the largest possible number.