Open In App
Related Articles

Java Program to Arrange given numbers to form the biggest number

Improve Article
Save Article
Like Article

Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value. And if the given numbers are {1, 34, 3, 98, 9, 76, 45, 4}, then the arrangement 998764543431 gives the largest value.

A simple solution that comes to our mind is to sort all numbers in descending order, but simply sorting doesn’t work. For example, 548 is greater than 60, but in output 60 comes before 548. As a second example, 98 is greater than 9, but 9 comes before 98 in output.

So how do we go about it? The idea is to use any comparison based sorting algorithm. 
In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers. 

Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260, we put Y first.

Following is the implementation of the above approach. 
To keep the code simple, numbers are considered as strings, the vector is used instead of a normal array. 

Below is the implementation of the above approach: 


// Given an array of numbers, program to
// arrange the numbers to form the
// largest number
import java.util.*;
class GFG 
    // The main function that prints the
    // arrangement with the largest value.
    // The function accepts a vector of strings
    static void printLargest(Vector<String> arr)
        Collections.sort(arr, new Comparator<String>()
            // A comparison function which is used by
            // sort() in printLargest()
            @Override public int compare(String X, 
                                         String Y)
                // First append Y at the end of X
                String XY = X + Y;
                // Then append X at the end of Y
                String YX = Y + X;
                // Now see which of the two
                // formed numbers is greater
                return XY.compareTo(YX) > 0 ? -1 : 1;
        Iterator it = arr.iterator();
        while (it.hasNext())
    // Driver code
    public static void main(String[] args)
        Vector<String> arr;
        arr = new Vector<>();
        // Output should be 6054854654
// This code is contributed by Shubham Juneja



Time Complexity:  O(nlogn) ,sorting is considered to have running time complexity of O(nlogn) and the for loop runs in O(n) time.
Auxiliary Space: O(1).

Please refer complete article on Arrange given numbers to form the biggest number | Set 1 for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 22 Dec, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials