Given an array of non-negative numbers(of Integer Range), they are needed to be arranged in some order such that it gives the max number. For example given array is A[1, 34, 3, 98, 9, 76, 45, 4, 12, 121]. if we arrange these numbers in the following order, A[9, 98, 76, 45, 4, 34, 3, 12, 121, 1], then by joining them we get “99876454343121211” as largest number.
Input : [1, 34, 3, 98, 9, 76, 45, 4, 12, 121] Output : 99876454343121211 Input : [12, 121] Output : 12121
In SET 1, we have discussed an approach to do same. In this set, we will discuss another logic.
1) Find number of digits in the largest number. Let number of digits be n.
2) Create extended version of all numbers. In extended version, we have n+1 digits formed by concatenating the number of with itself and truncating extra digits.
3) Sort original numbers according to their extended values.
4) Concatenating the sorted numbers produces th required result.
This article is contributed by Kondiparthi Shanmukha Sarath. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Arrange given numbers to form the biggest number | Set 1
- Arrange numbers to form a valid sequence
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Number of subarrays having sum of the form k^m, m >= 0
- Form minimum number from given sequence
- Form the largest number using at most one swap operation
- Form a number using corner digits of powers
- Form the smallest number using at most one swap operation
- Biggest integer which has maximum digit sum in range from 1 to n
- Form the largest palindromic number using atmost two swaps
- Arrange the array such that upon performing given operations an increasing order is obtained
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Number of subarrays with m odd numbers
- Number of n digit numbers that do not contain 9
- Number of occurrences of 2 as a digit in numbers from 0 to n
- Number of n digit stepping numbers
- Check if two numbers have same number of digits
- Sum of n digit numbers divisible by a given number
- Number of digits in the product of two numbers
- Sum of two numbers where one number is represented as array of digits