2 test cases are wrong

#include<bits/stdc++.h>
using namespace std;
int main()
{
int k;
int count=0;
int count1=0;
cin>>k;
string s;
cin>>s;
for(int i=0;i<s.length();i++)
{
if(s[i]==‘a’)
count1++;
else
count++;
}
if(count>count1)
{

    if(count1>k)
	   cout<<count+k;
    else
	   cout<<count+count1;


}
else
{
	   
    if(count>k)
	   cout<<count1+k;
    else
	   cout<<count+count1;
}

}

Hi @Shashank-Shekhar-2351214838461151

Your approach for this question is wrong. You can take two variable left and right and keep on increasing right till the count of a or b becomes greater than k then assign this to a variable answer, then increase the left variable.

Hope it Helps.

Your code will fail on test cases like : 2
bbbaaabbbab

Your program gives output 9 whereas correct answer is 6.

@Shashank-Shekhar-2351214838461151
Please mark your doubt as resolved.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.