temp = str[i];
str[i] = str[j];
str[j] = temp;
It is showing wrong answer (i donot able to find any problem in my code)
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int n = sc.nextInt(); String name[] = new String[n]; int salary[] = new int[n]; for(int i=0;i<n;i++){ name[i] = sc.next(); salary[i] = sc.nextInt(); } for (int i = 0;i<salary.length-1;i++) { for (int j = 0;j<salary.length-1-i;j++){ if(salary[j]<salary[j+1]){ int temp = salary[j]; salary[j]= salary[j+1]; salary[j+1] = temp; String temp1 = name[j]; name[j]= name[j+1]; name[j+1] = temp1; } } } for(int i= 0;i<name.length-1;i++){ for(int j = i+1;j<name.length;j++){ if(salary[i] == salary[j]){ if(name[i].compareTo(name[j])>0){ String temp2 = name[i]; name[i] = name[j]; name[j] = temp2; } } } } for(int i = 0;i<n;i++){ if(salary[i]>x){ System.out.println(name[i]+" "+salary[i]); } } } }
ur approach seems to be correct but it is not efficient
ur code will exceed the time limit
you are firstly sorting it on the basis of salary and again doing the same thing for the names
pls try to sort in only one sorting loop( both at the same time )
pls try to optimize ur code
but it is not showing time limit exceeded, it is showing wrong answer
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.
@8006366388,
In this question you have to use classes and objects to solve this question. We make our class implement comparator interface, in order to implement comparator interface we have to give body to compareTo function. In compareTo function we have to write the logic of sort function.
I’ve given you a start here. You just need to write code for comparator interface
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.