-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFlatLandSpaceStation.java
55 lines (46 loc) · 1.34 KB
/
FlatLandSpaceStation.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
52
53
54
55
// link: https://www.hackerrank.com/contests/worldcodesprint/challenges/powerplants-in-flatland
// name: FlatLand Space Station
import java.util.Scanner;
public class FlatLandSpaceStation {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
boolean[] stations = new boolean[n];
for (int i = 0; i < m; i++) {
int station = in.nextInt();
stations[station] = true;
}
int[] left = new int[n];
int[] right = new int[n];
if(!stations[0]){
left[0] = 10000000;
}
for (int i = 1; i < left.length; i++) {
if(stations[i]){
left[i]=0;
continue;
}
left[i]=left[i-1]+1;
}
if(!stations[n-1]){
right[n-1] = 10000000;
}
for(int i = right.length - 2; i>=0; i--){
if(stations[i]){
right[i]=0;
continue;
}
right[i]= right[i+1] +1;
}
int max = 0;
for(int i =0;i<n;i++){
int current = Math.min(left[i], right[i]);
if (current > max){
max = current;
}
}
if(max > n) max=n;
System.out.println(max);
}
}