-
Notifications
You must be signed in to change notification settings - Fork 6
/
Temperatures Solution (Java).java
43 lines (39 loc) · 1.24 KB
/
Temperatures Solution (Java).java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.*;
import java.io.*;
import java.math.*;
class Solution
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // the number of temperatures to analyse
in.nextLine();
String temps = in.nextLine(); // the n temperatures expressed as integers ranging from -273 to 5526
int[] arr = new int[n];
if (n == 0)
System.out.println(0);
else
{
for (int i = 0; i < n; i++)
{
if (temps.indexOf(" ") != -1)
{
arr[i] = Integer.parseInt(temps.substring(0, temps.indexOf(" ")));
temps = temps.substring(temps.indexOf(" ") + 1, temps.length());
}
else
{
arr[i] = Integer.parseInt(temps);
temps = "";
}
}
int max = 0;
for (int i = 0; i < n; i++)
{
if (Math.abs(arr[i]) < Math.abs(arr[max]) || (Math.abs(arr[i]) == Math.abs(arr[max]) && arr[i] > 0))
max = i;
}
System.out.println(arr[max]);
}
}
}