-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximizeDistanceToClosestPerson.java
More file actions
35 lines (27 loc) · 1010 Bytes
/
MaximizeDistanceToClosestPerson.java
File metadata and controls
35 lines (27 loc) · 1010 Bytes
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
public class MaximizeDistanceToClosestPerson {
public static void main(String[] args) {
int a[] = {0,0,1,0,0,0,0,0,0,0};
//int a[] = {0,0,1,0,0,0,0,0,1,0};
//int a[] = {0,0,1,0,0,1,0,0,1,0};
System.out.println(maxDistToClosest(a));
}
private static int maxDistToClosest(int[] a) {
int n = a.length;
int max = 0;
int i = 0;
while(i < n){
while(i < n && a[i] == 1){ //if its 1 then store its index
i++;
}
int start = i; //index stored in start
while(i < n && a[i] == 0){ //If its 0 go ahead
i++;
}
if(start == 0 || i == n) //if the first elementis 1 and 0 is present at last eg 1,0,0,0
max = Math.max(max, i - start); //directly caclu by doing i-start 3-0 = 3
else
max = Math.max(max, (i - start + 1) / 2); //eg 0,0,1,0,0 apply (i-start+1)/2
}
return max;
}
}