-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_Row.cpp
More file actions
53 lines (47 loc) · 1.04 KB
/
A_Row.cpp
File metadata and controls
53 lines (47 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(0);
int n;
string s;
cin >> n >> s;
// Check for adjacent 1s
for (int i = 0; i < n - 1; i++)
{
if (s[i] == '1' && s[i + 1] == '1')
{
cout << "No" << endl;
return 0;
}
}
// Check for isolated 0s
for (int i = 0; i < n; i++)
{
if (s[i] == '0')
{
bool hasAdjacentOne = false;
// Check left neighbor
if (i > 0 && s[i - 1] == '1')
{
hasAdjacentOne = true;
}
// Check right neighbor
if (i + 1 < n && s[i + 1] == '1')
{
hasAdjacentOne = true;
}
// If no adjacent 1, then this 0 is isolated
if (!hasAdjacentOne)
{
cout << "No" << endl;
return 0;
}
}
}
cout << "Yes" << endl;
return 0;
}