-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMostDistant.java
49 lines (40 loc) · 1.29 KB
/
MostDistant.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
// https://www.hackerrank.com/challenges/most-distant
// T: O(N)
// S: O(1)
import java.util.Scanner;
public class MostDistant {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int N = SCANNER.nextInt();
int xMin = 0, xMax = 0, yMin = 0, yMax = 0;
xMin = xMax = SCANNER.nextInt();
yMin = yMax = SCANNER.nextInt();
for (int i = 1 ; i < N ; i++) {
final int x = SCANNER.nextInt();
final int y = SCANNER.nextInt();
yMin = Math.min(yMin, y);
yMax = Math.max(yMax, y);
xMin = Math.min(xMin, x);
xMax = Math.max(xMax, x);
}
double distance = max(
xMax - xMin,
yMax - yMin,
distance(xMin, yMin),
distance(xMin, yMax),
distance(yMin, xMax),
distance(yMax, xMax)
);
System.out.println(distance);
}
private static double distance(int a, int b) {
return Math.sqrt(a * a + b * b);
}
private static double max(double... numbers) {
double result = Double.MIN_VALUE;
for (double number : numbers) {
result = Math.max(result, number);
}
return result;
}
}