Why am i getting WA

#include<bits/stdc++.h>
using namespace std;

int main(void)
{
int t,i,j,count;
float atd,q,n;
char a[1000];

cin>>t;
while(t–)
{
q=i=j=count=0;
cin>>n;
cin>>a;
for(i=0;i<n;i++)
{
if (a[i]==‘P’)
q++;
}
atd=q/n;
for(j=2;j<n-2;j++)
{
if(a[j]==‘A’&& (a[j+1]==‘P’||a[j+2]==‘P’ )&&(a[j-1]==‘P’||a[j-2]==‘P’))
{ q++;
count++;
atd=q/n;
}
if (atd>=0.75)
break;
else
continue;
}
if(atd<0.75)
cout<<"-1"<<"\n";
else
cout<<count<<"\n";
}

// your code goes here
return 0;

}
PLEASE TELL ME WHY AM I GETTING WA

what will happen if atd=0.75?

Your code fails when the condition is always satisfied.

Case:
1
9
PAAPPPPPP

And as pointed out by @debarya_452 , it should be atd>=0.75

the question was atleast 0.75 i.e. it should be atd>=0.75 at break line

yea ik i corrected that, but still it shows wa

ik i corrected that , but it still shows wa

pointed out by @satyankar_2005 first check if attendence is already 0.75 or not but your code starts with if condition which will first check proxy so your count may be greater than the optimal answer
ex
PAAPPPPPP
ythe attendance is already 0.75 but
your code will see for 3rd letter A
count will++ hence count will be 1 which should be 0 otherwise

1 Like

Floating points might create a problem.
since we are dealing with integers here in both input and output try to use integer.
Rather then doing atd>=0.75, do something like P>=3*A

I CORRECTED WHAT YOU SAID , HAVE A LOOK , IT STILL SHOWS WA-
#include<bits/stdc++.h>
using namespace std;

int main()
{
int t,i,j,count;
float atd,q,n;
char a[1000];

cin>>t;
while(t–)
{
q=i=j=count=0;
cin>>n;
cin>>a;
for(i=0;i<n;i++)
{
if(a[i]==‘P’)
q++;
}
atd=q/n;
if(atd<0.75)
{
for(j=2;j<n-2;j++)
{
if(a[j]==‘A’&& (a[j+1]==‘P’||a[j+2]==‘P’ )&&(a[j-1]==‘P’||a[j-2]==‘P’))
{ q++;
count++;
atd=q/n;
}
if (atd>=0.75)
break;
else
continue;
}

if(atd<0.75)
cout<<"-1"<<"\n";
else
cout<<count<<"\n";
}
else
cout<<0;
}

// your code goes here
return 0;
}