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:
- Handling Large Inputs (Not Likely an Issue, But Good to Check).
- Handling Edge Case: n = 0 (Empty Input).
What I expected:
- Fixes potential crash when n = 0 (by checking if (n == 0) return;).
What actually happened:
- Nothing, the problem still persists.
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:
- Handling Large Inputs (Not Likely an Issue, But Good to Check).
- Handling Edge Case: n = 0 (Empty Input).
What I expected:
- Fixes potential crash when n = 0 (by checking if (n == 0) return;).
What actually happened:
- Nothing, the problem still persists.
- 1 "integers -10¹⁸ < ai < 10¹⁸" ... try inputting a very large number (e.g. 10¹⁷) and see what happens – teapot418 Commented Mar 11 at 11:55
- Where in the universe can you find a temperature of -10¹⁸? – k314159 Commented Mar 11 at 15:03
1 Answer
Reset to default 0The 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
.