PRFYIT - Editorial

i aggree with u bro

#include<bits/stdc++.h>

using namespace std;

#define ll long long

int main(){

ios_base::sync_with_stdio(false);

cin.tie(NULL);

ll t;

cin>>t;

while(t--){

    string s;

    cin>>s;

    ll biggest_bunch1=0;

    ll biggest_bunch0=0;

    ll count0=0, count1=0;

    ll total_count0=0;

    ll total_count1=0;

    for(unsigned ll i=0;i<s.length();i++){

        if(s[i]=='0'){

            count1=0;

            count0++;  

            biggest_bunch0=max(biggest_bunch0, count0);   

            total_count0++;

        }

        else if(s[i]=='1'){

            count0=0;

            count1++;

            biggest_bunch1=max(biggest_bunch1, count1);

            total_count1++;

        }

    }

    // cout<<"0->"<<biggest_bunch0<<endl;

    // cout<<"1->"<<biggest_bunch1<<endl;

    cout<< min(total_count0-biggest_bunch0, total_count1-biggest_bunch1)<<endl;

}

return 0;

}

Can anyone tell me where does my code fail??

Well, please post your logic Read here

i still didn’t get how Mn is giving answer

For which test case will this fail ?
Please help…
Language : C++14
#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl “\n”
using namespace std;

bool checklast(ll i, ll diff, ll ans[], ll ans2[])
{
ll j;
for(j=1; j<diff-1; j++)
{
if(ans[i]==ans2[j])
{
ans2[j]=-1;
return 0;
}
}
return 1;
}

int main()
{
fastio
ll t;
cin>>t;
while(t–)
{
string a;
cin>>a;
ll i;
ll n=a.size();
ll diff=1;
for(i=0; i<n-1; i++)
{
if(a[i+1]!=a[i])
{
diff++;
}
}
if(diff<=3)
{
cout<<0<<endl;
continue;
}
ll ans[diff];
ll x=0;
ll k=1;
for(i=0; i<n-1; i++)
{
if(a[i+1]==a[i])
{
k++;
}
else
{
ans[x++]=k;
k=1;
}
}
ans[x++]=k;
ll ans2[diff];
copy(ans, ans+diff, ans2);
sort(ans, ans+diff);
ll g=0;
ll flag=4;
ll left=diff;
for(i=0; left>3; i++)
{
if(checklast(i, diff, ans, ans2))
{
if(diff%2==0 && flag==4)
{
g+=ans[i];
left-=1;
flag=3;
}
else
{
continue;
}
}
else
{
g+=ans[i];
left-=2;
}
}
ll neww=i;
ll cont=0;
for(; i>=0; i–)
{
if(checklast(i, diff, ans, ans2))
{
cont++;
}
}
if(cont==2 && diff%2!=0)
{
g-=ans[neww-1];
g+=ans2[0];
g+=ans2[diff-1];
}
cout<<g<<endl;
}
return 0;
}

Read this

how it tells about the no of element to be deleted

P[w][i - 1] + (j - i + 1) - (P[w][j] - P[w][i - 1]) + (P[w][n] - P[w][j])

How to format my code ?

Wrap your code in backquotes(`), or post a link to your submission.

Please explain wrapping the code in backquotes.

Simply enclose it in backquotes.

I tried it, but it didn’t help. See my last post’s edit history.

Try using three backquotes; such as (triple backquote) (newline) (code) (newline) (triple backquote)

Or add 4 spaces in the beginning to each line

i simple found the largest continuous bunch of 0s and 1s and subtracted it from the total no of 0s and 1s respectively and took the minimum of both
for example
10111000010
this has the longest continuous bunch of 0s as 4
and longest continous bunch of 1s and 3
so the ans would be min of 6-4, 5-3 which is 2

thankyou so much :slight_smile:

In this line,

shows number of w characters in [1,L-1]

shows number of 1-w characters in range [L,R]

shows number of w characters in [R+1, N]

Correct me if wrong !

1 Like

1
11111111111100001000010000111111111100000
Your Output is 12 but answer should be 7 by removing 2-1 in mid and 5-0 at last
1111111111110000000000001111111111

1 Like

Oh good, didn’t notice that :slight_smile: ; but you should be able to prove the correctness of your solution(which I think it is correct)! Can you?

Please explain with an example.it would be a great help. please

bro please explain your code in detail.please