#36 Maximize Distance to the Closest Person

Companies:
VK Yandex
Themes:
AlgorithmsArraysPractice

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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in