import java.io.*;
import java.util.*;
import java.lang.*;
class Codechef {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int dishes = sc.nextInt();
int arr[] = new int[dishes];
for(int i = 0; i < dishes; i++) {
if(sc.hasNextInt()) {
arr[i] = sc.nextInt();
}
}
Arrays.sort(arr);
int a = 0;
int b = 0;
for(int i = dishes - 1; i >= 0; i--) {
if(a >= b) b += arr[i];
else a += arr[i];
}
System.out.println(a > b ? a : b);
}
}
}
I’m getting this error while solving the problem. Where to edit the code to handle this error? Please help.
This problem only arises when you try only running the code without any custom input, whereas if you would try running your cone on some custom input or directly submitting the code the error might not show.
And you are taking input in the array the wrong way. You are overriding the first array element everytime you take an array element input because of the while loop. The while loop is totally unnecessary here.