Python – Smallest integer possible from combination of list elements

Given a list of integers, the task is to get smallest integer possible from the combination of list elements. This is one of the problems that is essential in a competitive point of view and this article discusses various shorthands to solve this problem in Python. Letâ€™s discuss certain ways in which this problem can be solved.

Method #1 : Using sorted() + lambda The combination of the above function can be used to perform this task. The sorted function performs the sort of list indices converted into string and lambda functions handle the conversion and iteration operation.

Python3

 `# Python code to demonstrate ``# Smallest number from list``# using sorted() + lambda``import` `functools` `# initializing list ``test_list ``=` `[``23``, ``65``, ``98``, ``3``, ``4``]` `# printing original list``print` `("The original ``list` `is` `: " ``+` `str``(test_list))` `# lambda for custom operation``custom ``=` `lambda` `i, j: ``-``1` `if` `str``(j) ``+` `str``(i) > ``str``(i) ``+` `str``(j) ``else` `1` `# using sorted() + custom function``# Smallest number from list ``res ``=` `sorted``(test_list, key ``=` `functools.cmp_to_key(custom))`  `# printing result ``print` `("The smallest possible number : " ``+` `"".join(``map``(``str``, res)))`

Output :
```The original list is : [23, 65, 98, 3, 4]
The smallest possible number : 23346598```

Time Complexity: O(nlogn) where n is the number of elements in the string list. The sorted() + lambda is used to perform the task and it takes O(nlogn) time.
Auxiliary Space: O(1) additional constant space is required

Method #2 : Using itertools.permutation() + join() + min() The itertools.permutation can be used to get possible permutation and min function chooses the minimum of it after being converted to integer as a result of joined output as given by join function.

Python3

 `# Python3 code to demonstrate ``# Smallest number from list``# using itertools.permutation() + join() + min()``from` `itertools ``import` `permutations` `# initializing list ``test_list ``=` `[``23``, ``65``, ``98``, ``3``, ``4``]` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))` `# using itertools.permutation() + join() + min()``# Smallest number from list``res ``=` `int``(``min``((''.join(i) ``for` `i ``in` `permutations(``str``(i) ``                     ``for` `i ``in` `test_list)), key ``=` `int``))` `# printing result ``print` `(``"The smallest possible number : "` `+` `str``(res))`

Output
```The original list is : [23, 65, 98, 3, 4]
The smallest possible number : 23346598```

Time complexity: O(n), where n is the length of the numbers list. The itertools.permutation() + join() + min() has a time complexity of O(n)

Auxiliary Space: O(n),where n is the length of the numbers list.

Method #3 : Using functools.reduce() function

Step by Step Algorithm

1. Initialize a list of integers called test_list with some values.
2. Print the original list using the print() function.
3. Sort the integers in the list in ascending order using the sorted() function and store the result in a variable called sorted_list.
4. Use the functools.reduce() function to repeatedly concatenate the integers in the sorted list. The resulting value is the smallest possible number that can be formed from the integers in the original list.
5. Print the smallest possible number using the print() function.

Note : The reduce() function applies the lambda function to each element of the list in turn, cumulatively applying the operation to the current result and the next element of the list. The lambda function takes two arguments, x and y, which represent two adjacent elements of the list. It concatenates the two elements as strings and compares the numerical values of the resulting two concatenated numbers. If the first concatenated number is smaller, the function returns x, otherwise it returns y.

Python3

 `# Python code to demonstrate ``# Smallest number from list``# using functools.reduce()` `import` `functools` `# initializing list ``test_list ``=` `[``23``, ``65``, ``98``, ``3``, ``4``]` `# printing original list``print``(``"The original list is:"``, test_list)` `# sort the integers in the list in ascending order``sorted_list ``=` `sorted``(test_list)` `# use functools.reduce() to repeatedly concatenate the integers in the list``min_concat ``=` `functools.``reduce``(``lambda` `x, y: ``int``(``str``(x) ``+` `str``(y)) ``if` `int``(``str``(x) ``+` `str``(y)) < ``int``(``str``(y) ``+` `str``(x)) ``else` `int``(``str``(y) ``+` `str``(x)), sorted_list)` `# printing result ``print``(``"The smallest possible number:"``, min_concat)`

Output
```The original list is: [23, 65, 98, 3, 4]
The smallest possible number: 23346598```

Complexity Analysis :

Time Complexity: O(nlogn), This is because of the use of the built-in sorted() function which takes O(n log n) time.
Space Complexity: O(n), This is because of the use of the sorted_list variable, which takes O(n) space to store the sorted list. The use of functools.reduce() and the lambda function takes constant space.

Previous
Next