Python program to print sorted number formed by merging all elements in array
Last Updated :
22 Apr, 2023
Given an array arr[], the task is to combine all the elements in the array sequentially and sort the digits of this number in ascending order.
Note: Ignore leading zeros.
Examples:
Input: arr =[7, 845, 69, 60]
Output: 4566789
Explanation: The number formed by combining all the elements is "78456960" after sorting the digits we get 4566789
Input: arr =[8, 5603, 109, 53209]
Output: 1233556899
Explanation: The number formed by combining all the elements is "8560310953209" after sorting the digits we get "1233556899"
Approach 1:
- Convert each element of the list to a string using map() function.
- Join the list using join() function.
- Sort the string using join() and sorted()
- Convert string to an integer using type casting
- Return the result
Below is the implementation of the above approach:
Python3
# Python program to print sorted number by merging
# all the elements in array function to print
# sorted number
def getSortedNumber(number):
# sorting the string
number = ''.join(sorted(number))
# converting string to integer
number = int(number)
# returning the result
print(number)
# function to merge elements in array
def mergeArray(lis):
# convert the elements of list to string
lis = list(map(str, lis))
# converting list to string
string = ''.join(lis)
# passing this string to sortednumber function
getSortedNumber(string)
# Driver code
lis = [7, 845, 69, 60]
# passing list to merge array function to merge
# the elements
mergeArray(lis)
Time complexity: O(nlogn) because sorting the string in the getSortedNumber function takes O(nlogn) time, where n is the length of the string.
Auxiliary space: O(n), where n is the number of elements in the input list.
Approach 2: Using list(),str(),extend(),sort(),join() and int() methods
Python3
# Python program to print sorted number by merging
# all the elements in array function to print
# sorted number
lis = [7, 845, 69, 60]
p = []
for i in lis:
x = list(str(i))
p.extend(x)
p.sort()
p = "".join(p)
print(int(p))
Time complexity: O(n log n), where n is the total number of digits in the input list.
Auxiliary space: O(n), where n is the total number of digits in the input list.
Approach#3:using itertools
Algorithm
- Convert each number in the input array to a string.
- Concatenate all the strings to form a single string.
- Convert the merged string to a list of integers.
- Sort the list of integers and join them back into a single string.
- Convert the sorted string to an integer and return it.
Python3
import itertools
# Function to perform the merge sort
def merge_sort(arr):
# Convert each number in the
# array to a string
str_arr = [str(num) for num in arr]
# Concatenate all the strings to
# form a single string
merged_str = ''.join(str_arr)
# Convert the merged string to a
# list of integers
merged_list = [int(char) for char in merged_str]
# Sort the list of integers and
# join them back into a single string
sorted_str = ''.join([str(num) for num in sorted(merged_list)])
# Convert the sorted string to an
# integer and return it
return int(sorted_str)
# Driver Code
arr = [7, 845, 69, 60]
result = merge_sort(arr)
print(result)
Time Complexity: O(n log n), where n is the length of the input array. This is because we convert the array to a single string in O(n) time, sorting the string takes O(n log n) time, and converting the sorted string back to an integer takes O(n) time.
Space Complexity: O(n), where n is the length of the input array. This is because we need to store the merged list of integers, which has a length of n. The sorted string also has a length of n. The permutations are no longer generated and stored, so the space complexity is improved compared to the previous approach.
Similar Reads
Python Program to Print uncommon elements from two sorted arrays Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements ar
3 min read
Python program to insert an element into sorted list Inserting an element into a sorted list while maintaining the order is a common task in Python. It can be efficiently performed using built-in methods or custom logic. In this article, we will explore different approaches to achieve this.Using bisect.insort bisect module provides the insort function
2 min read
Python3 Program for Merge 3 Sorted Arrays Given 3 arrays (A, B, C) which are sorted in ascending order, we are required to merge them together in ascending order and output the array D. Examples: Input : A = [1, 2, 3, 4, 5] B = [2, 3, 4] C = [4, 5, 6, 7]Output : D = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7]Input : A = [1, 2, 3, 5] B = [6, 7, 8,
8 min read
Python program to sort digits of a number in ascending order Given an integer N, the task is to sort the digits in ascending order. Print the new number obtained after excluding leading zeroes. Examples: Input: N = 193202042Output: 1222349Explanation: Sorting all digits of the given number generates 001222349.Final number obtained after removal of leading 0s
2 min read
Python Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ..... Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR
3 min read
Python Program To Merge K Sorted Linked Lists - Set 1 Given K sorted linked lists of size N each, merge them and print the sorted output. Examples: Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->7->8->9->10-
6 min read
Python Program To Merge Two Sorted Lists (In-Place) Given two sorted lists, merge them so as to produce a combined sorted list (without using extra space).Examples: Input: head1: 5->7->9 head2: 4->6->8 Output: 4->5->6->7->8->9 Explanation: The output list is in sorted order. Input: head1: 1->3->5->7 head2: 2->4 Output: 1->2->3->4->5->7 Explanation: T
5 min read
Python Program to Find Largest Number in a List Finding the largest number in a list is a common task in Python. There are multiple way to do but the simplest way to find the largest in a list is by using Python's built-in max() function:Using max()Python provides a built-in max() function that returns the largest item in a list or any iterable.
3 min read
Python program to list Sort by Number value in String Given a List of strings, the task is to write a Python program to sort list by the number present in the Strings. If no number is present, they will be taken to the front of the list. Input : test_list = ["gfg is 4", "all no 1", "geeks over 7 seas", "and 100 planets"] Output : ['all no 1', 'gfg is 4
6 min read
Python Program to extracts elements from the list with digits in increasing order Given a List of elements, extract all elements which have digits that are increasing in order. Input : test_list = [1234, 7373, 3643, 3527, 148, 49] Output : [1234, 148, 49] Explanation : All elements have increasing digits.Input : test_list = [12341, 7373, 3643, 3527, 1481, 491] Output : [] Explana
5 min read