You are given an array representing a row of seats where seats[i] = 1 represents a person sitting in the ith seat, and seats[i] = 0 represents that the ith seat is empty (0-indexed).

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.&nbsp;

Return that maximum distance to the closest person.

&nbsp;
Example 1:

Input: seats = [1,0,0,0,1,0,1]
Output: 2
Explanation: 
If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.


Example 2:

Input: seats = [1,0,0,0]
Output: 3
Explanation: 
If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.


Example 3:

Input: seats = [0,1]
Output: 1


&nbsp;
Constraints:


	2 &lt;= seats.length &lt;= 2 * 104
	seats[i]&nbsp;is 0 or&nbsp;1.
	At least one seat is empty.
	At least one seat is occupied.


class Solution:
    def maxDistToClosest(self, seats: List[int]) -> int:
        # strategy is greedy solution:
        # calculate local maximum for each interval: (b-a)//2
        # then take max of local maximums
        # the solution is O(n)
        # I find this solution clear, but uses 5 passes
        
        # get all the occupied seat nums
        seat_nums = [ix for ix, val in enumerate(seats) if val == 1]
        
        # check the ends
        left_max, right_max = min(seat_nums), len(seats)-max(seat_nums)-1
        
        # calculate max distance for each gap
        dists = [(y-x)//2 for x, y in zip(seat_nums, seat_nums[1:])]
        
        # take max of sitting on either end + each gap
        return max([left_max, right_max, *dists])
class Solution {
    public int maxDistToClosest(int[] seats) {
        int size = seats.length;
        int max = 0;
        int start = -1;
        int end = -1;

        for(int i = 0; i<size; i++){
            if(seats[i] != 0){
                start = end; // update start to end when we have a filled seat.
                end = i; // update end with i pointer when we have a filled seat.
                if(start == -1) max = i; // for special case when there is only '1' in the array
                else max = Math.max((end-start)/2,max); // updating max.
            }
        }

        // Handeling speical cases before returning max.
        // 1) last element is 0 as we wont be updating max for that in above loop.
        // 2) when there only single '1' in the array, we need to make sure whether right half is bigger than the left half.

        if(seats[size - 1] == 0 || start == -1) return Math.max(max, (size - 1 - end));
        return max;
    }
}
class Solution {
public:
    int maxDistToClosest(vector<int>& seats) {
        vector<int> d;
        int cnt = -1, ans = 0;

        for(int i=0; i<seats.size(); i++) {
            cnt++;
            if(seats[i]) d.push_back(cnt), cnt = 0;
        }
        d.push_back(cnt);

        for(int i=0; i<d.size(); i++) {
            if(i > 0 && i < d.size() - 1) d[i] /= 2;
            ans = max(ans, d[i]);
        }

        return ans;
    }
};
/**
 * @param {number[]} seats
 * @return {number}
 */
var maxDistToClosest = function(seats) {
    let arr = seats.join('').split('1');
    for(let i = 0; i < arr.length;i++){
        if(arr[i] == '')
            arr[i] = 0;
        else{
            let middle = true;
            if(i == 0 || i == arr.length-1){
                arr[i] = arr[i].length;
            }else {
                arr[i] = Math.ceil(arr[i].length/2);
            }
        }
    }
    return arr.sort((a,b) => (a >= b)?-1:1)[0]
};
Maximize Distance to Closest Person