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.
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.
// Java program to arrange the numbers to form the
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 email@example.com. 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.
Please Login to comment...