Getting WA

CodeChef: Practical coding for everyone ← this is the problem link!!

int main()

{

ios::sync_with_stdio(0);

cin.tie(0); cout.tie(0);

ll T;

cin >> T;

while (T--) {

    ll n, k;

    read(n, k);

    string s;

    cin >> s;

    vl vec(k, 0);

    Fo(i, k) cin >> vec[i];

    ll ans=0;

    fo(i, 0, n-1) {

        if (s[i]==s[i+1]) {

            ans+=2;

        } else {

            ans++;

        }

    }

    Fo(i, k) {

        ll pos=vec[i]-1;

        if (pos==0) {

            if (s[pos]==s[pos+1]) {

                s[0]=(s[0]=='0') ? '1' : '0';

                cout << ans-1 << '\n';

                ans--;

                continue;

            } else {

                s[0]=(s[0]=='0') ? '1' : '0';

                cout << ans+1 << '\n';

                ans++;

            }

            }else if (pos==n-1) {

                if (s[pos]==s[pos-1]) {

                    s[pos]=(s[pos]=='0') ? '1' : '0';

                    cout << ans-1 << '\n';

                    ans--;

                } else {

                    s[pos]=(s[pos]=='0') ? '1' : '0';

                    cout << ans+1 << '\n';

                    ans++;

                }

            } else {

                if (s[pos]==s[pos+1] && s[pos]==s[pos-1]) {

                    s[pos]=(s[pos]=='0') ? '1' : '0';

                    ans-=2;

                    cout << ans << '\n';

                } else if (s[pos]!=s[pos+1] && s[pos]!=s[pos-1]) {

                    s[pos]=(s[pos]=='0') ? '1' : '0';

                    ans+=2;

                    cout << ans << '\n';

                } else {

                    s[pos]=(s[pos]=='0') ? '1' : '0';

                    cout << ans << '\n';

                }

            }

        }

    }

}

hi,can u please provide full code,including macro part?

1 Like

logic is correct,i wanna TEST this code,what is wrong in that code.

1 Like

thanks bro, i got the correct output after i posted here, the mistake was i didnt include the edge case when there was only one subatomic particle :joy:

Ok