Facing runtime error in chef and proxy problem

//this is my code please help me find the mistake.
#include <bits/stdc++.h>

using namespace std;

int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,count=0,sum=0;
cin>>n;
string s;
cin>>s;
for(int j=0;j<n;j++)
{
if(s[j]==‘P’)
{
count++;
}
else if(s[j]==‘A’)
{
if((s[j-1]==‘P’||s[j-2]==‘P’) && (s[j+1]==‘P’||s[j+2]==‘P’))
{
sum++;
}
}
}
if(count>=ceil(n0.75))
{
cout<<0;
}
else{
if(sum>=ceil(n
0.75)-count)
{
cout<<ceil(n*0.75)-count<<endl;
}
else
{
cout<<-1<<endl;
}
}

}
return 0;

}

I think it’s giving run time error because of this line “ceil(n0.75)” . I think it should be (n*0.75).

Try this https://repl.it/repls/OffshoreAchingTruetype

But i m not sure about WA though.

No, actually this is not the error, I don’t why it is appearing here as (n0.75) even after editing, but in the actual code which I submitted there it is (n*0.75).

did u try the above link?

Your code will give RE for this test case :

1
5
APPPP

@ rachna_27 Problem in this line
[if((s[j-1]==‘P’||s[j-2]==‘P’) && (s[j+1]==‘P’||s[j+2]==‘P’))]

Your code try to find the value of s[-2] and s[-1] which is invalid.
@jjtomar gives you the testcase .

yes, it’s working there.

but the code runs properly in my ide then why not here?
also, i did this correction " s[-1]=‘0’;s[-2]=‘0’;s[n]=‘0’;s[n+1]=‘0’;". I added this line at the top after taking input from the string, if at all this was the problem, still it is showing runtime error(segmentation fault).

I think there is a misconception. You cannot refer to index -1 or greater than length of array because it does not exist in memory. So writing s[-1]=0 won’t work. It will throw a run-time error. If it’s running in your machine, I’m sure it is because of a different compiler. This IS a logical error in your code.

P.S. I would recommend that you add link to problem when asking doubts about solution of it. It is really hard to track the original problem to refer to it.

changing this line to

if((s[ ( ( (j-1) >= 0)? (j-1) : 0 ) ]==‘P’||s[( ( (j-2) >= 0)? (j-2) : 0 ) ]==‘P’) && (s[ ( ( (j+1) < n)? (j+1) : n-1 ) ]==‘P’||s[ ( ( (j+2) < n)? (j+2) : n-1 ) ]==‘P’))

should do the trick.

i’m so sorry…this is the problem link

//this is the updated solution which is not working, this is giving wrong answer.
#include <bits/stdc++.h>

using namespace std;

int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,count=0,sum=0;
cin>>n;
char s[1004];
for(int j=0;j<n;j++)
{
cin>>s[j];
}
s[-1]=‘0’;s[-2]=‘0’;s[n]=‘0’;s[n+1]=‘0’;
for(int j=0;j<n;j++)
{
if(s[j]==‘P’)
{
count++;
}
else if(s[j]==‘A’)
{
if((s[j-1]==‘P’||s[j-2]==‘P’) && (s[j+1]==‘P’||s[j+2]==‘P’))
{
sum++;
}
else
{continue;}
}
}
if(count>=ceil(n0.75))
{
cout<<0;
}
else{
if(sum>=ceil(n
0.75)-count)
{
cout<<ceil(n*0.75)-count<<endl;
}
else
{
cout<<-1<<endl;
}
}

}
return 0;

}