Problem · Array

Maximize Distance to Closest Person

EasyRobloxPHONE SCREEN
See Roblox hiring insights

You are given an array seats where seats[i] = 1 means seat i is occupied and seats[i] = 0 means it is empty. There is at least one empty seat and at least one occupied seat.

Alex wants to sit so that the distance between him and the closest occupied seat is maximized. Return that maximum distance.

The distance to the closest person at an empty seat is the minimum gap to an occupied seat on either side. The best seat is either in the middle of the longest run of consecutive empty seats (distance (gap) / 2) or at one of the two ends (distance equal to the leading or trailing run of empty seats).

Examples
01 · Example 1
seats = [1, 0, 0, 0, 1, 0, 1]
return = 2
Sitting at index 2 puts Alex 2 seats away from the closest occupied seat (indices 0 and 4).
02 · Example 2
seats = [1, 0, 0, 0]
return = 3
Sitting at the last seat puts Alex 3 seats away from the only occupied seat at index 0.
03 · Example 3
seats = [0, 1]
return = 1
Sitting at index 0 puts Alex distance 1 from the occupied seat at index 1.
Constraints
  • 2 <= seats.length <= 2 * 10^4
  • seats[i] is 0 or 1.
  • At least one seat is empty.
  • At least one seat is occupied.
More Roblox problems
drafts saved locally
public int maxDistToClosest(int[] seats) {
  // write your code here
}
seats[1, 0, 0, 0, 1, 0, 1]
expected2
sign in to submit