Skip to main content
2 of 2
Clarified leading zeros.
user avatar
user avatar

Largest and smallest values from concatenated integers

This problem is from Five programming problems every Software Engineer should be able to solve in less than 1 hour which itself is an interesting read. The first few problems are trivial, but the fourth one can be a bit more interesting.

Given a list of integers separated by a single space on standard input, print out the largest and smallest values that can be obtained by concatenating the integers together on their own line.

For example:

Input:

5 56 50

Output:

50556
56550

Various points of order:

  • The order of the results are smallest then largest.
  • Only the smallest and largest values may be printed out (iterating over all the variations and printing them out isn't valid).
  • There will always be two or more integers in the list.
  • It is possible for the largest and smallest results to be the same. In the case of input 5 55, the number 555 should be printed twice.
  • The integers are not necessarily distinct. 5 5 is valid input.
  • Leading 0s on integers are not valid input. You will not need to account for 05 55.

As this is code golf, shortest entry wins.

user12166