You're given a row with n chairs. We call a seating of people "maximal" if the two following conditions hold:
There are no neighbors adjacent to anyone seated.
It's impossible to seat one more person without violating the first rule.
The seating is given as a string consisting of zeros and ones (0 means that the corresponding seat is empty, 1 — occupied). The goal is to determine whether this seating is "maximal".
Note that the first and last seats are not adjacent (if n ≠ 2).
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of chairs.
The next line contains a string of n characters, each of them is either zero or one, describing the seating.
Output
Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No".
You are allowed to print letters in whatever case you'd like (uppercase or lowercase).
Examples
Input
3
101
Output
Yes
Input
4
1011
Output
No
Input
5
10001
Output
No
Note
In sample case one the given seating is maximal.
In sample case two the person at chair three has a neighbour to the right.
In sample case three it is possible to seat yet another person into chair three.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to check if the seating is maximal as defined: no adjacent occupied seats (neighbors adjacent) and it's impossible to seat one more person without violating that rule.
So given a binary string s of length n. Check two conditions:
For all i, if s[i]=='1', then s[i-1] != '1' and s[i+1] != '1' (unless out of bounds). This ensures no two adjacent ones.
There is no empty seat '0' that can be changed to '1' without creating adjacency. That is, there is no index i such that s[i]=='0' and both neighbors (i-1 and i+1) are not '1' (consider boundaries appropriately). Actually condition: we cannot seat one more person without violating rule 1. So if there exists any index i with s[i]=='0' and (i==0 or s[i-1]!='1') and (i==n-1 or s[i+1]!='1'), then it's possible to seat a person at i, so seating is not maximal. Conversely, if no such empty seat exists, then it's maximal (provided condition 1 holds). However note: even if condition 1 fails, we should output No because it's not maximal? The problem says: "We call a seating of people "maximal" if the two following conditions hold: 1. There are no neighbors adjacent to anyone seated. 2. It's impossible to seat one more person without violating the first rule." So both conditions must hold. So if condition 1 fails, seating is not maximal (output No). If condition 1 holds but condition 2 fails, output No. Only if both hold, output Yes.
Edge cases: n=1: string can be "0" or "1". Check: For "1": condition1 holds (no neighbor). condition2: can we seat one more? No seats left, so impossible => maximal? Actually there is no empty seat, so we cannot seat another. So condition2 holds? But check: is there an index i with s[i]=='0'? There is none, so no. So maximal => Yes. For "0": condition1 holds (trivially). condition2: there is a seat '0' with no adjacent occupied neighbors (since none), so we could seat someone there => not maximal => No.
n=2: Note: The first and last seats are not adjacent? "Note that the first and last seats are not adjacent (if n ≠ 2)." That implies when n=2, chairs 1 and 2 are adjacent? The note says: "the first and last seats are not adjacent (if n ≠ 2)." So for n=2, they ARE adjacent? Actually typical interpretation: chairs are in a row, adjacency is next to each other. So chairs 1 and 2 are neighbors. For n=1, there is no neighbor. For n=2, they are neighbors. For n>2, first and last are not neighbors because they are at ends. So adjacency definition: two chairs are adjacent if they are next to each other in the row, not considering wrap-around. So we just treat adjacency as indices differ by 1. So for n=2, chairs 0 and 1 are adjacent. That matches the note: "Note that the first and last seats are not adjacent (if n ≠ 2)." Means that for n=2, they are adjacent because they are the only two seats and they are next to each other. So in our adjacency check, it's just i and i+1.
So algorithm: read n, s. Convert to list.
First, check condition1: iterate i from 0 to n-1. If s[i]=='1', then if i>0 and s[i-1]=='1' or i<n-1 and s[i+1]=='1', then condition1 fails => print No and exit.
If passes condition1, then check condition2: iterate i from 0 to n-1. If s[i]=='0', check left: (i==0 or s[i-1]!='1') and right: (i==n-1 or s[i+1]!='1'). If true, then there exists an empty seat that can be occupied without violating adjacency (since neighbors are not occupied). So seating not maximal => print No and exit.
If after both passes we have not printed No, then print Yes.
But careful: condition2: "impossible to seat one more person without violating the first rule." So if there is any empty seat where both adjacent seats (if exist) are not occupied, we can seat there. That's correct. But consider that seating a person might create adjacency with existing ones, but we only require that after seating, the rule (no adjacent) holds. So we need both left and right not occupied. So condition is exactly as described.
Edge cases: if the string has a '1' with adjacent '1's, condition1 fails, thus not maximal. That's correct.
Examples:
3, "101": condition1: indices: 0:1 neighbor (1) is 0 ok; 1:0; 2:1 neighbor (1) is 0 ok. So passes. Condition2: check zeros: index1=0; left is s[0]=1 (occupied), so cannot seat because neighbor is occupied => not available. So no empty seat that can be taken => maximal => Yes.
4, "1011": condition1: index2=1 has neighbor index3=1 -> adjacent => fails, so No.
5, "10001": condition1: check ones at 0 and 4? Actually s[0]=1, s[4]=1, no adjacent ones because they are far. So passes. Condition2: zeros: indices 1,2,3 are 0. Check index1: left s0=1 (occupied) so not available. Index2: left s1=0 not occupied? Actually left is index1=0, which is empty, not occupied; right index3=0, not occupied. But careful: condition for seating requires that after seating, there is no adjacency. If we put a person at index2, its neighbors are index1 and index3, both are 0, not occupied. That would be fine. So seat available => not maximal => No. Output matches.
So solution straightforward.
Implementation: read int n, read string s. Possibly strip whitespace.
Complexity O(n). n <=1000.
Let's double-check tricky cases: "000" n=3: condition1 passes (no ones). Condition2: Check any zero: index0: left none, right s1=0 not occupied, so can seat => not maximal => No. That's correct: it's not maximal because you can seat someone.
"010" n=3: condition1: index1=1, neighbors are 0 and 0, ok. Condition2: zeros: index0: left none, right s1=1 is occupied, so cannot seat? Actually neighbor right is occupied, so seating at index0 would be adjacent to index1 => violates rule, so not allowed. Index2: left s1=1 occupied, so not allowed. So no seat available => maximal => Yes. But does that satisfy? Is it indeed maximal? With seating "010", you cannot seat another without creating adjacency: seat at 0 adjacent to 1, seat at 2 adjacent to 1, seat at 1 is occupied. So yes. So output Yes. That's correct.
Another: "0" n=1: condition1 passes. Condition2: index0 is 0 and no neighbors, so can seat => not maximal => No.
Now n=2: Test "00": condition1 passes (no ones). Condition2: index0: left none, right s1=0 not occupied => can seat? But after seating at index0, the seating becomes "10". Check adjacency: index0=1, index1=0 (not adjacent). So it's allowed. So there exists a seat => not maximal => No. That seems correct because you could also seat at index1.
"01": condition1: index1=1, check neighbor left (index0=0) ok, so passes. Condition2: zeros: index0=0: left none, right s1=1 is occupied => cannot seat because adjacent => no seat? But is there any other zero? Only index0. So no available seat? Actually, seating at index0 would be adjacent to index1 (occupied), so not allowed. So condition2 says no available seat, so maximal? But is "01" maximal? Let's see: seating "01" has no adjacent ones. Can we seat another person? Only empty seat is index0. If we seat at index0, we get "11", which has adjacent ones (violates). So cannot seat another. So it is maximal. So output Yes. Similarly "10" => Yes. "11": condition1 fails => No.
So logic holds.
Note: In some problems, they may consider that seating a person at an empty seat cannot be adjacent to any occupied seat; that's what we implemented.
Thus solution is simple.
Edge: maybe need to consider that condition2 requires that it's impossible to seat one more person while still satisfying condition1. If condition1 already fails, seating is not maximal regardless. So we can first check condition1; if fails, output No; else check condition2.
Implementation details: Python.
Let's write.
Pseudo:
def solve():
import sys
data = sys.stdin.read().split()
n = int(data[0])
s = data[1].strip()
# condition1
for i,ch in enumerate(s):
if ch == '1':
if i>0 and s[i-1]=='1' or i<n-1 and s[i+1]=='1':
print("No")
return
# condition2
for i,ch in enumerate(s):
if ch == '0':
left_ok = (i==0 or s[i-1]!='1')
right_ok = (i==n-1 or s[i+1]!='1')
if left_ok and right_ok:
print("No")
return
print("Yes")
That's it.
Double-check: What about n=0? Not possible.
Test sample:
Sample1: 3 101 -> condition1 ok; zeros at index1: left 1 occupied -> not both left_ok and right_ok? left_ok? i=1 not 0, s[i-1]='1' -> left_ok false (since s[i-1]!='1'? Actually left_ok = (i==0 or s[i-1]!='1') -> false because s[i-1]=='1', so condition fails). So no output No from condition2. Then print Yes.
Potential nuance: In condition2, when checking for a zero, we need to ensure that after seating, the person doesn't have neighbors occupied. But also need to ensure that the new occupancy doesn't cause adjacency between the new person and another person? That's exactly checking neighbors. But what about adjacency between existing persons? The existing seating already satisfied condition1, so no adjacency among them. Adding a person at a zero where neighbors are not occupied ensures the new person is not adjacent to any existing. However, could adding a person at a zero create adjacency between two existing persons? No, because adjacency only cares about immediate neighbors; adding a person at a seat does not change adjacency between other seats. So it's fine.
Thus solution stands.
We can also do alternative: After condition1, check if there's any three consecutive zeros? Actually not exactly. But our approach works.
Let's ensure reading input correctly: string length might be exactly n, but we don't need to check. Use strip.
Time O(n). Memory O(1).
We'll produce final answer.
But we are to write in a conversational assistant style? The problem expects solution code. Since the user asked "You are ChatGPT... solve the problem." Usually we need to output solution. But the conversation context: "You are given a problem... etc. Write a solution." We should provide explanation and code. The final answer should contain the code. So we'll output Python solution.
We'll also consider using fast I/O, but not needed.
Edge: n=1000, fine.
Implementation in Python:
def main():
import sys
input_data = sys.stdin.read().split()
if not input_data:
return
n = int(input_data[0])
s = input_data[1]
# check condition1
for i in range(n):
if s[i] == '1':
if (i > 0 and s[i-1] == '1') or (i < n-1 and s[i+1] == '1'):
print("No")
return
# check condition2
for i in range(n):
if s[i] == '0':
if (i == 0 or s[i-1] != '1') and (i == n-1 or s[i+1] != '1'):
print("No")
return
print("Yes")
if name == "main":
main()
Yes.
Now provide final answer with explanation and code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
s = data[1].strip()
code
# condition 1: no two adjacent occupied seats
for i, ch in enumerate(s):
if ch == '1':
if (i > 0 and s[i-1] == '1') or (i < n-1 and s[i+1] == '1'):
print("No")
return
# condition 2: cannot seat another person
for i, ch in enumerate(s):
if ch == '0':
left_ok = (i == 0 or s[i-1] != '1')
right_ok = (i == n-1 or s[i+1] != '1')
if left_ok and right_ok:
print("No")
return
print("Yes")