# Beat Seat

<figure><img src="/files/trBrFHlg9P8MA6v0Exgq" alt=""><figcaption></figcaption></figure>

* n, 1

```jsx
function bestSeat(seats) {

  let s = 0;
  let e = 0;

  let seat = [-1, -1];
  while (e < seats.length) {
    let left = seats[s];
    let right = seats[e];

    if (left === 1) {
      s++;
      e++;
      continue;
    }

    if (right === 1) {
      const [m, length] = calcSeat(s, e - 1);
      if (seat[1] < length) {
        seat = [m, length];
      }
      
      s = e;
      e += 1;
      continue;
    }

    e++;
  }
  
  return seat[0];
}

const calcSeat = (s, e) => {
  return [s + Math.floor((e - s) / 2), e - s];
}

// Do not edit the line below.
exports.bestSeat = bestSeat;
```

```jsx
function bestSeat(seats) {

  let beatSeat = 0;
  let maxSeats = 0;

  let left = 0;
  while (left < seats.length) {
    let right = left + 1;
    while (right < seats.length && seats[right] === 0) {
      right++;
    }

    const availableSeats = right - left - 1;
    if (maxSeats < availableSeats) {
      beatSeat = left + Math.floor((right - left) / 2);
      maxSeats = availableSeats;
    }

    left = right;
  }

  
  return beatSeat;
}

// Do not edit the line below.
exports.bestSeat = bestSeat;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/beat-seat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
