Given an array “A” of positive integers. Your task is to make the largest number possible formed by concatenating each of the array elements exactly once.
Example:
Let's say the given array is [ 9, 98, 7].
All the possible numbers formed by concatenating each of the array elements are 7989,7998,9879,9897,9987,9798. Among the six numbers, 9987 is the greatest number. Hence the answer is 9987.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
Then the T test cases follow.
The first line of each test case contains an integer 'N' denoting the number of elements in the array.
The second line of each test case contains 'N' space-separated integers denoting the array elements.
Output format:
For each test case, print the largest number possible formed by concatenating each of the array elements exactly once.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1<= T <=10
1<= N <=10^3
0<= A[i] <=10^9
Time Limit: 1sec
Using the inbuilt library of Python, itertools library can be used to perform this task.
The idea is to try out all the possible answers one by one and find out the largest one among all of them.
To implement this approach, we will generate all the permutations of the ...read more
The idea is to build an auxiliary array in which all the array elements should have the same number of digits so that we can simply sort the auxiliary array in descending order and concatenate ...read more
To solve this problem is to sort the array in descending order, but the sorting list/array does not work here. For example, 213 is greater than 75 but in a sorted array, 75 comes after 213. And this would produce the number 21375. But the largest number that can be formed is 75213. therefore sorting will not work here. But we can modify the comparator function of sort().
The idea is to use any of the comparison-based sorting algorithm
- We will write our own custom comparator function for sorting.
- For the two numbers A and B. This custom function will not compare the A and B with each other. But it compares AB with BA and the greater number will come first in sorted order.
- Here AB means the number is formed by appending A to B vice versa for BA.
For Example A = 213 and B = 75 then AB = 21375 and BA = 75213.To compare A and B our custom compare function will compare AB and BA, Since AB < BA so, we will put B first in sorted list/array.
Space Complexity: OtherExplanation:O(N * log10(MAX)), where ‘N’ is the number of elements in the array and MAX is the maximum among the array elements.
In the worst case, we need to store only the maximum possible answer. So the overall space complexity is O(N * log10(MAX)).
Time Complexity: OtherExplanation:O(N * log(N) * log10(MAX)), where ‘N’ is the number of elements in the array and MAX is the maximum among the array elements.
In the worst case, it takes O(N * log(N)) to sort an array of N elements and it takes log10(MAX) to compare two elements using the comparator function. Hence, the overall time complexity is O(N * log(N) * log10(MAX)).
Top Tata 1mg Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month