-
Notifications
You must be signed in to change notification settings - Fork 54
/
Java.java
51 lines (43 loc) · 1.48 KB
/
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
44
45
46
47
48
49
50
51
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: Temperatures */
/* Difficulty: Easy */
/* Date solved: 08.11.2018 */
/* */
/****************************************/
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//Read inputs.
Scanner scanner = new Scanner(System.in);
int N = Integer.parseInt(scanner.nextLine());
String[] inputs = scanner.nextLine().split(" ");
//If there are no temperatures provided, output 0.
if (N <= 0)
{
System.out.println(0);
return;
}
int closestToZero = Integer.MAX_VALUE;
for (int i = 0; i < N; i++)
{
int T = Integer.parseInt(inputs[i]);
//Find the closest to zero.
if (Math.abs(T) < Math.abs(closestToZero))
{
closestToZero = T;
}
//Find the positive number that is closest to zero instead of the negative number (see 5 and -5).
else if (Math.abs(T) == Math.abs(closestToZero))
{
closestToZero = Math.max(closestToZero, T);
}
}
//Print output.
System.out.println(closestToZero);
}
}