import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0){
int n = cin.nextInt(), arr[] = new int[n];
for(int i =0;i<arr.length;i++){
arr[i] = cin.nextInt();
}
printLargest(arr);
t–;
}
}
public static void printLargest(int [] a){
for(int count = 0;count<a.length-1;count++){
for(int j = 0 ;j<a.length-1-count;j++){
String s2 = Integer.toString(a[j]), s3 = Integer.toString(a[j+1]);
StringBuilder s1 = new StringBuilder(s2);
s1.append(s3);
StringBuilder s4 = new StringBuilder(s3);
s4.append(s2);
if(Integer.parseInt(s1.toString())<Integer.parseInt(s4.toString())){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int s :a){
System.out.print(s);
}
}
}=====what is error in code
Problem biggest number problem
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.
you have to use inbuild sort function otherwise you will get timelimit .
i have already used the approach you said but what sort funaction are you talking about?
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.