my code is showing error of runtime error must be because of array indexing but i am not able to crack the error in my code.
suppose i take an example as
changes->2
string->abbaaabaaaa
and the maximum length of the substring is 9…
this is as follows… max when iterated from string is 1 2 3 4 5 6 (6-2+1)=5 6 7 8 9…
(6-2+1) because of 3rd b where I have to nullify the first b which occur at position 2…
my code:
#include
#include
using namespace std;
int main()
{
string str;
int n;
cin>>n;
cin>>str;
int max=0;
int curr=0;
int n1=0;
int s=str.length();
int arr[s];
int j=0;
int k=0;
for(int i=0;i<str.length();i++)
{
if(str.at(i)==‘a’)
{
curr++;
}
else
{
if(n1<n)
{
curr++;
n1++;
arr[j]=curr;
j++;
}
else
{
curr=curr-arr[k]+1;
k++;
arr[j++]=curr;
}
}
if(max<curr)
{
max=curr;
}
}
cout<<max<<endl;
curr=0;
n1=0;
int ar[s];
j=0;
k=0;
for(int i=0;i<str.length();i++)
{
if(str.at(i)==‘b’)
{
curr++;
}
else
{
if(n1<n)
{
curr++;
n1++;
ar[j++]=curr;
}
else
{
curr=curr-ar[k]+1;
k++;
ar[j++]=curr;
}
}
if(max<curr)
{
max=curr;
}
}
cout<<max;
return 0;
}