Alternate Seating
Greedy Allocation / Boundary CheckSolved Nov 9, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven N people needing seats and an array of M seats (0=vacant, 1=occupied), find if all N people can be seated such that no two people sit next to each other (i.e., new person must have empty seats on both sides, if they exist).ReadHide
Problem Description
Given N people needing seats and an array of M seats (0=vacant, 1=occupied), find if all N people can be seated such that no two people sit next to each other (i.e., new person must have empty seats on both sides, if they exist).
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Iterate through the seats and, at each vacant spot (0), check if it meets the no-neighbor condition (both left and right are empty). If it does, 'seat' a person there (by setting the seat to 1) and decrement the required people count N.
Approaches
1. Greedy Single Pass (Optimal) - O(M) ⭐
Idea: The best strategy is to be greedy: whenever a valid empty spot is found, take it immediately, as taking a spot can only improve the chances for future people by reducing the required count N.
- Iterate through the seats
M. - At index
i, check the three conditions for a valid seat:- Seat is empty (
seats[i] == 0). - Left neighbor is empty (or index
i=0). - Right neighbor is empty (or index
i=M-1).
- Seat is empty (
- If valid, set
seats[i] = 1and decrement the required people countN. - If
Nreaches 0, the answer is YES. If the loop finishes withN > 0, the answer is NO.
Time: O(M) (A single pass over the seats).
Space: O(1) (Modification done in place).
Solution code
import sys
def solve_alternate_seating():
# Read N (people needed)
try:
N = int(sys.stdin.readline())
except:
return
# Read M (number of seats)
try:
M = int(sys.stdin.readline())
except:
return
# Read the array elements
try:
seats = list(map(int, sys.stdin.readline().split()))
except:
return
people_seated = 0
# Optimal: O(M) Time - Greedy Single Pass
for i in range(M):
if people_seated >= N:
break
# Check 1: Seat must be vacant
if seats[i] == 0:
# Check 2: Left neighbor must be empty (or boundary)
left_empty = (i == 0) or (seats[i-1] == 0)
# Check 3: Right neighbor must be empty (or boundary)
right_empty = (i == M - 1) or (seats[i+1] == 0)
if left_empty and right_empty:
# Greedily take the seat
seats[i] = 1
people_seated += 1
if people_seated >= N:
print("YES")
else:
print("NO")
if __name__ == "__main__":
solve_alternate_seating()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language