Hello,
I have written the follwing code for this question and I am not able to understand why 2 of the testcases are failing for this code as I have tried the dry run and it should work fine as per me, so can you kindly guide me where am I making mistake ?
#include
#include
#include
using namespace std;
int largest(int var_1, int var_2)
{
if(var_1>var_2)
{
return var_1;
}
else
{
return var_2;
}
/else{
return var_1;
}/
}
int count_b_func(int k, char temp[])
{
int len = strlen(temp);
int count_b = 0;
int a = 0;
for (int i =0;i<=len-1;i++)
{
if(temp[i]==βbβ)
{
count_b++;
}
if(temp[i]==βaβ)
{
a++;
}
}
if(k>a)
{
count_b = count_b+a;
}
else if(k<=a)
{
count_b = count_b+k;
}
return count_b;
}
int count_a_func(int k, char temp[])
{
int len = strlen(temp);
int count_a = 0;
int b = 0;
for (int i =0;i<=len-1;i++)
{
if(temp[i]==βaβ)
{
count_a++;
}
if(temp[i]==βbβ)
{
b++;
}
}
if(k>b)
{
count_a = count_a+b;
}
else if(k<=b)
{
count_a = count_a+k;
}
return count_a;
}
int main() {
int len_a =0;
int len_b =0;
int k;
cin>>k;
cin.get();
char a[1000];
char temp[1000];
cin.getline(a,1000);
int len = strlen(a);
int j;
int a_num;
int b_num;
int perf;
//1.getting substrings
for(int i=0;i<=len-1;i++)
{
int x=0;
for(j =i;j<=len-1;j++)
{
temp[x++] = a[j]; //substr stored in temp arr.
}
temp[j] = '\0';
//cout<<temp<<endl;
a_num = count_a_func(k,temp);
//cout<<"No of a "<<a_num<<endl;
b_num = count_b_func(k,temp);
//cout<<"No of b "<<b_num<<endl;
len_a = largest(a_num,len_a);
len_b = largest(b_num,len_b);
}
perf = largest(len_a,len_b);
cout<<perf;
return 0;
}