Ctrl K

A Seat Far From Everyone

Medium
A row of seats in a hall is described by the array row: row[i] = 1 if seat i is taken and 0 if it is free. The row has at least one free seat and at least one taken seat. You want to take a free seat so that the distance to your nearest neighbour is as large as possible. The distance between seats i and j is |i - j|. Return that largest possible distance.
Example 1
row=[1, 0, 0, 0, 0, 0, 1, 0, 1] → 3

Taking seat 3 leaves the nearest neighbour 3 seats away on both sides. Nothing does better.

Example 2
row=[0, 0, 0, 0, 1, 1] → 4

The best choice is the very end, seat 0: the nearest person is 4 seats away.

Example 3
row=[1, 0] → 1
Constraints
  • 2 ≤ len(row) ≤ 10⁵
  • row[i] is 0 or 1
  • At least one seat is free and at least one is taken
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumNext Arrangement in Order Medium
loading Python…
Input
row=[1, 0, 0, 0, 0, 0, 1, 0, 1]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 123 tests on the server, hidden ones included.