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

#define FIO ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const ll INF = 1e18;

int main()
{
    FIO
    int n, ans = 0;
    vector<int> a[2];
    string s;
    cin >> n >> s;
    for (int i = 0; i < n; i++)
        a[s[i] - '0'].push_back(i);
    for (int j = 0; j < 2; j++)
        for (int i = 0; i < a[j].size() - 1; i++)
            ans = max(ans, a[j][i] + (n - a[j][i + 1] - 1));
    cout << ans << '\n';
    return 0;
}
Copy
Pop Pop Mariam_Yasser
GNU G++17
1 ms
512 KB
Wrong Answer