Source Code
#include <bits/stdc++.h>
using namespace std;

int main() {
	
	int n;
	cin>>n;
	string s;
	cin>>s;
	
	bool first = s[0]=='1';
	bool second = s[s.size()-1]=='1';
	
	set<int> pos[2];
	for(int i=0;i<n;i++) {
		pos[s[i]-'0'].insert(i);
	}
	
	int ans = 0;
	for(int i=0;i<n;i++) {
		if(i!=0)
			pos[s[i-1]-'0'].erase(i-1);
		if(s[i]-'0' != first)
			continue;
		if(pos[second].size()==0)
			continue;
		int nearest = *(pos[second].begin());
		ans = max(ans, n - (nearest-i+1));
	}
	
	cout<<ans<<endl;
	
	return 0;
}
Copy
Pop Pop Mr-Spy
GNU G++17
20 ms
5.5 MB
Accepted