I have implemented the Java code well and am getting correct outcomes on 33% of the test outcomes. On others I got WA (Wrong Answer)
Can someone please tell me what am I doing wrong.
class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] vals = br.readLine().split(" ");
int n = Integer.parseInt(vals[0]);
int k = Integer.parseInt(vals[1]);
int[] list1 = new int[n];
int[] list2 = new int[n];
int[] maxlist = new int[n];
int[] minlist = new int[n];
for(int i = 2;i<n+2;i++){//Insertion Sort Shelf 1
int key = Integer.parseInt(vals[i]);
list1[i-2] = key;
int j = i-1;
while(j>=2 && list1[j-2]>key){
list1[j+1-2] = list1[j-2];
j--;
}
list1[j+1-2] = key;
}
for(int i = n+2;i<(n+n+2);i++){//Insertion Sort Shelf 2
int key = Integer.parseInt(vals[i]);
list2[i-(n+2)] = key;
int j = i-1;
while(j>=n+2 && list2[j-(n+2)]>key){
list2[j+1-(n+2)] = list2[j-(n+2)];
j--;
}
list2[j+1-(n+2)] = key;
}
String l1 = "";
String l2 = "";
for(int i = 1;i<n+1;i++){//I need to know which is a bigger list
if(list1[n-1]>list2[n-1]){
maxlist = list1;
minlist = list2;
break;
}else if(list1[n-1]<list2[n-1]){
maxlist = list2;
minlist = list1;
break;
}else{
if(i==n+1){//The lists are hell same
maxlist = list2;
minlist = list1;
break;
}//Or just wait
}
}
for(int i=0;i<k;i++){//Something really GREEDY
int a = minlist[n-1];//7 Ignore
int b = maxlist[0];//2 Ignore
if(b<a){
minlist[n-1] = b;
maxlist[0] = a;
Arrays.sort(minlist);
Arrays.sort(maxlist);
}
}
System.out.println(String.valueOf(minlist[n-1]+maxlist[n-1]));
}
}
Any help is appreciated. Thanks in advance. Could not eradicate myself as I could not view the test inputs and other people’s codes.