Finding the second largest element of large input sets I have a problem where I need to find the second maximum element of the user inputs. It's an online practice problem and I can't figure out why the server responds back with a Non-Zero Exit Code error when I submit my code. The  program produces right outputs every single time when tested locally. Can someone tell me which part of my code is prone to such errors and how I can deal with this error?

Sample Input:                 Sample Output:

2                             7
1 3 5 7 8 -1                  16
12 23 16 0 2 -1


The first input is the number of test cases. Second and third inputs are the actual user inputs respectively. And user inputs are always terminated with -1.

Here is my code:

class SecondLargest {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int testcase=Integer.parseInt(sc.nextLine());

        while(testcase--&gt;0){
            ArrayList&lt;Long&gt; al=new ArrayList&lt;&gt;();
            long num=0;
            long i=0;
            while((num=(sc.nextLong()))!=-1){
                al.add(num);
                i++;
            }
            Collections.sort(al);
            if(al.size()==0){
                System.err.println("");
            }
            else if(al.size()==1){
                System.out.println(al.get(0));
            }
            else if(al.size()&gt;1){
            System.out.println(al.get((int)(al.size()-2)));
            }
        }
    }
}


Large input sets will be used to test the program.
So your code seems to work fine, but you get a non-zero exit code for some unknown inputs... What could go wrong? If I call out to you 100 numbers, do you need to remember all of them to tell me the second highest? (inside an array in your head) 
You don't. And the program doesn't either.
Most probably the program crashes with an out of memory error.

Here's one way to solve this using constant storage:

    while (testcase-- &gt; 0) {
        long num;
        long largest = Long.MIN_VALUE;
        long secondLargest = Long.MIN_VALUE;
        while ((num = sc.nextLong()) != -1) {



                  if (num > largest) {
                     secondLargest = largest;
                     largest = num;
                } else if (num > secondLargest) {
                     secondLargest = num;
                }  


        }
        System.out.println(secondLargest);
    }


Code review

Your code had serious formatting issues. Cleaned up, your code would look like this:

    while (testcase-- &gt; 0) {
        List&lt;Long&gt; numbers = new ArrayList&lt;&gt;();
        long num;
        while ((num = sc.nextLong()) != -1) {
            numbers.add(num);
        }
        Collections.sort(numbers);
        if (numbers.size() == 0) {
            System.err.println("");
        } else if (numbers.size() == 1) {
            System.out.println(numbers.get(0));
        } else {
            System.out.println(numbers.get(numbers.size() - 2));
        }
    }


What changed?


Formatting fixed
Use interface type instead of implementation (List)
Removed redundant variables
Removed redundant initializations
Removed redundant parentheses
Improved variable names