Construct the Largest Number from the Given Array in Java
In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with one another once you provide the array's numbers as input. It will then draw a conclusion.
The topic is the most well-known challenge that major IT organisations (including Apple, Google, Amazon, and Microsoft) typically ask candidates about during the coding section of the interview. We'll write Java programmes (using various logics) in this section to extract the greatest number from an array.
For instance, enter 10, 68, 75, 7, 21, and 12.
Produced by: 77568211210
The biggest number that may be generated from the above-mentioned array is 77568211210. It's crucial to remember that it doesn't matter what sequence we combine the integers together in.
The Problem's Solution
The intended result cannot be achieved by simply sorting the arrays in decreasing order and taking that sorted order to be the desired result since the integer may not be the greatest number.
The pairwise comparisons (XY and YX) will be compared first, and the bigger number will be displayed first in the sorted order. Next, we will develop a unique comparator method for the sorting process. Notably, comparing Y and X together is not necessary in this case.
BN.java
//importing the required packages
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class BN
{
// function that builds the largest number
public static String findBN(List<Integer> nums)
{
//sort using a custom function object
Collections.sort(nums, (a, b) -> (String.valueOf(b) + a).compareTo(String.valueOf(a) + b));
return nums.stream().map(Object::toString).collect(Collectors.joining(""));
}
//driver code
public static void main(String args[])
{
List<Integer> numz = Arrays.asList(7, 55,86,5,18, 34, 54, 0, 90);
String BN = findBN(numz);
System.out.println("The largest number is: " + BN);
}
}
Output:
The largest number present is: 908775854342160
The aforementioned program's complexity is O (nlogn). where the input size (n) is.
Here is another justification for the same.
The following Java application creates a string out of each entry in an array. It will be beneficial to convert numbers into string if indeed the array includes more numbers since when we add up each number, we get a big amount. The array was then sorted (in descending order). When an array is sorted in descending order, there may be an issue if a group of numbers have the same starting digits.
Consider the array 3, 30, 34, 5, 9 as an illustration.
9534303 is created as the greatest number when the aforementioned array is sorted in descending order. However, that is not the right response. You may get the right response by flipping the numbers 33 and 3030.
As a result, when shorting, evaluate (for each pair - wise comparisons) the result of appending the pair in the two different orders. The intended outcome is obtained as the greatest integer once the array has been sorted.
There could be a circumstance when an array is made up entirely of zeros. If 00 is the largest number in this situation, then return 00. Otherwise, return the greatest value from the sorted array.
Let's put the aforementioned reasoning into a Java application.
LargerNumberComparator.java
import java.util.*;
public class LargestNumber {
private class LargerNumberComparator implements Comparator<String> {
@Override
public int compare(String a, String b) {
S o1 = x + y;
S o2 = y + x;
return o2.compareTo(o1);
}
}
// function that determines the array's biggest number
public String BN(int[] num) {
//getting up the input integers as in strings
String[] asStrs = new String[num.length];
for (int i = 0; i < num.length; i++) {
aStrs[i] = String.valueOf(num[i]);
}
Arrays.sort(aStrs, new BNComparator());
if (aStrs[0].equals("0")) {
return "0";
}
String BNS = new String();
for (String numAsStr : asStrs) {
BNS += numAsStr;
}
return BNS;
}
public static void main(String s[]) {
int aa[] = { 7, 22,89,36,14, 54, 0, 90 };
BN ln = new BN();
String s = ln.lBN(aa);
System.out.println("The constructed largest number in array is:" +s);
}
}
Output:
The constructed largest number in array is:908784864342160
Because we used more space to store the integers, the time complexity for the aforementioned method is O(nlogn) as well as the space complexity is O(n).