I need a solution of this problem in java. And can we do this question using 2D array? If yes, please tell how to do?
Not able to solve
Sort Game Problem
hi @minal.251298
in java you just need to write custom sort and access the editorial for the solution of this problem.
if you still want java solution let me know
Yes I do need the code…if u can provide me with the solution…
ok @minal.251298
here is the code if you can have any doubt regarding the code you can ask
import java.util.*;
public class testing {
public static class Employee implements Comparable {
int marks;
String name;
Employee(int marks, String name) {
this.marks = marks;
this.name = name;
}
@Override
public int compareTo(Employee o) {
if (this.marks == o.marks) {
return this.name.compareTo(o.name);
}
return o.marks - this.marks;
}
}
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int x=scn.nextInt();
int n=scn.nextInt();
Employee em[]=new Employee[n];
for(int i=0;i<n;i++){
String name=scn.next();
int marks=scn.nextInt();
em[i]=new Employee(marks,name);
}
Arrays.sort(em);
for(int i=0;i<n;i++){
if(em[i].marks>=x){
System.out.println(em[i].name+" "+em[i].marks);
}
}
}
}
Thanks a lot…!!
Can u please check my code??
Its giving wrong answer…
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press “Run” button to execute it.
******************************************************************************/
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 marks[]= new int[n];
for(int i=0;i<n;i++)
{
name[i]=sc.next();
marks[j]=sc.nextInt();
}
int temp;
String temps;
String s1="";
String s2="";
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(marks[j]==marks[j+1])
{
s1=name[j];
s2=name[j+1];
if(s1.charAt(0)>s2.charAt(0))
{
temp=marks[j];
marks[j]=marks[j+1];
marks[j+1]=temp;
temps=name[j];
name[j]=name[j+1];
name[j+1]=temps;
}
}
if(marks[j]<marks[j+1])
{
temp=marks[j];
marks[j]=marks[j+1];
marks[j+1]=temp;
temps=name[j];
name[j]=name[j+1];
name[j+1]=temps;
}
}
}
for(int i=0;i<n;i++)
{
if(marks[i]<x)
{
continue;
}
else
{
System.out.println(name[i]+" "+marks[i]);
}
}
}
}
hi @minal.251298
you code is not working because you are checking only the first character of the string there might be cases when first character is equal and the rest of the string is different.
for eg aman and apaar.
so inorder to check which string is lexicographically smaller you have loop over the string but this will increase the time complexity of your program.
why my solution is giving run error in this??
hello @minal.251298
you are having run error becoz of this
for(int i=0;i<n;i++)
{
name[i]=sc.next();
marks[j]=sc.nextInt();
}
it should have marks[i] but you have written marks[j] correct that
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 marks[]= new int[n];
for(int i=0;i<n;i++)
{
name[i]=sc.next();
marks[i]=sc.nextInt();
}
int temp;
String temps;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(marks[j]==marks[j+1])
{
if(name[j].compareTo(name[j+1])>0)
{
temp=marks[j];
marks[j]=marks[j+1];
marks[j+1]=temp;
temps=name[j];
name[j]=name[j+1];
name[j+1]=temps;
}
}
if(marks[j]<marks[j+1])
{
temp=marks[j];
marks[j]=marks[j+1];
marks[j+1]=temp;
temps=name[j];
name[j]=name[j+1];
name[j+1]=temps;
}
}
}
for(int i=0;i<n;i++)
{
if(marks[i]<x)
{
break;
}
else
{
System.out.println(name[i]+" "+marks[i]);
}
}
}
}
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.