javaruntime-error

My solution for a Kattis problem (Hitastig) keep returning a Run-Time Error


I'm trying to solve the Kattis problem: Hitastig

The task is simply to print the highest and lowest temperatures from a given list of temperature values. Everytime I submit my solution I keep failing on test case 26/31 where the only details I get on the error is that my code produced a "Run-Time Error". My solution generates the correct solutions for the sample inputs and it runs fine in my personal IDE.

Is there something blatant I'm missing here? What could be wrong in my code that results in this error?

Any help here would be appreciated.

Thanks in advance.

import java.util.Scanner;

public class hitastig {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        int n = myScanner.nextInt();
        int maxTemp = Integer.MIN_VALUE;
        int minTemp = Integer.MAX_VALUE;

        for (int i = 0; i < n; i++) {
            int temp = myScanner.nextInt();
            maxTemp = Math.max(maxTemp, temp);
            minTemp = Math.min(minTemp, temp);
        }

        System.out.println(maxTemp + " " + minTemp);
        myScanner.close();
    }
}

I've tried:

What I expected:

What actually happened:


Solution

  • The problem specifies "integers -10¹⁸ < ai < 10¹⁸" which is beyond the range of int (int goes up to 2*10⁹-ish). It seems to fit into long, so you can try to change the type from int to long and the reading of input accordingly. Or there is always BigInteger.