#36 Maximize Distance to the Closest Person
Seats in a movie theater are arranged in one row. To sit as far as possible from the other viewers in the row, a newly arrived viewer chooses a seat where the distance to the nearest viewer is as large as possible. It is guaranteed that there is always at least one free seat in the row and that at least one viewer is already seated. Write a function that returns the distance from the selected seat to the nearest viewer for the given row of seats, represented by an array of zeros and ones.
// Input: [1, 0, 0, 0, 1]
// Output: 2
// Input: [1, 0, 1, 0, 1, 0, 0, 0, 1]
// Output: 2
// Input: [1, 0, 1, 0]
// Output: 1
public static int maxDistance(int[] seats) {
...
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor