Open In App

# Python Program to Arrange given numbers to form the biggest number

Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value. And if the given numbers are {1, 34, 3, 98, 9, 76, 45, 4}, then the arrangement 998764543431 gives the largest value.

A simple solution that comes to our mind is to sort all numbers in descending order, but simply sorting doesn’t work. For example, 548 is greater than 60, but in output 60 comes before 548. As a second example, 98 is greater than 9, but 9 comes before 98 in output.

So how do we go about it? The idea is to use any comparison based sorting algorithm.Â
In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers.Â

Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260,Â we put Y first.

Following is the implementation of the above approach.Â
To keep the code simple, numbers are considered as strings, the vector is used instead of a normal array.Â

Below is the implementation of the above approach:Â

## Python3

 `# Python3 Program to get the maximum``# possible integer from given array``# of integers...`` ` ` ` `# Custom comparator to sort according``# to the ab, ba as mentioned in description``def` `comparator(a, b):``    ``ab ``=` `str``(a) ``+` `str``(b)``    ``ba ``=` `str``(b) ``+` `str``(a)``    ``return` `((``int``(ba) & gt``             ``int``(ab)) ``-``            ``(``int``(ba) & lt``             ``int``(ab)))`` ` `def` `myCompare(mycmp):`` ` `    ``# Convert a cmp= function into a ``    ``# key= function``    ``class` `K(``object``):``        ``def` `__init__(``self``, obj, ``*``args):``            ``self``.obj ``=` `obj`` ` `        ``def` `__lt__(``self``, other):``            ``return` `mycmp(``self``.obj, other.obj) & ``                   ``lt ``0`` ` `        ``def` `__gt__(``self``, other):``            ``return` `mycmp(``self``.obj, other.obj) & ``                   ``gt ``0`` ` `        ``def` `__eq__(``self``, other):``            ``return` `mycmp(``self``.obj, other.obj) ``=``=` `0`` ` `        ``def` `__le__(``self``, other):``            ``return` `mycmp(``self``.obj, other.obj) & ``                   ``lt ``=` `0`` ` `        ``def` `__ge__(``self``, other):``            ``return` `mycmp(``self``.obj, other.obj) & ``                   ``gt ``=` `0`` ` `        ``def` `__ne__(``self``, other):``            ``return` `mycmp(``self``.obj, other.obj) !``=` `0``    ``return` `K`` ` ` ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``a ``=` `[``54``, ``546``, ``548``, ``60``]``    ``sorted_array ``=` `sorted``(a, key ``=` `                   ``myCompare(comparator))``    ``number ``=` `"".join([``str``(i) ``for` `i ``in` `sorted_array])``    ``print``(number)`` ` `# This code is Contributed by SaurabhTewary`

Output:

`6054854654`

Time Complexity:Â  O(nlogn) ,sorting is considered to have running time complexity of O(nlogn) and the for loop runs in O(n) time.
Auxiliary Space: O(1).

Another approach:(using itertools)Â

Using the inbuilt library of the Python, itertools library can be used to perform this task.Â Â

## Python3

 `# Python3 implementation this is to ``# use itertools. permutations as ``# coded below:`` ` `from` `itertools ``import` `permutations``def` `largest(l):``    ``lst ``=` `[]``    ``for` `i ``in` `permutations(l, ``len``(l)):`` ` `        ``# Provides all permutations of the ``        ``# list values, store them in list to ``        ``# find max``        ``lst.append("".join(``map``(``str``,i))) ``    ``return` `max``(lst)`` ` `print``(largest([``54``, ``546``, ``548``, ``60``])) ``# This code is contributed by Raman Monga`

Output:

`6054854654`

Time Complexity:Â  O(nlogn)
Auxiliary Space: O(1).

Please refer complete article on Arrange given numbers to form the biggest number | Set 1 for more details!