Source Code
#include <bits/stdc++.h>

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

typedef long long ll;
typedef long double ld;
const int N = 1e3 + 15, lg = 20;
int mod = 1e9 + 7;
int inf = 1e9;


int main() {
    FIO
    int n;
    string s;
    cin >> n >> s;
    int ans = 0;
    set<int> st[2];
    for (int i = 0; i < n; i++)
        st[s[i] - '0'].insert(i);
    for (auto it:st[s[0] - '0']) {

        auto it2 = st[s.back() - '0'].lower_bound(it);
        if (it2 != st[s.back() - '0'].end()) {
            ans = max(ans, n-*it2 + it-1);
        }


    }
    cout << ans << endl;

    return 0;
}
Copy
Pop Pop ELbadawe
GNU G++17
27 ms
5.5 MB
Accepted