Where did I go wrong? My code gives answer but shows WA. Problem: https://www.codechef.com/problems/PROXYC

#include
using namespace std;

int main() {
// your code goes here
int t;
cin >> t;
while(t–){
int d,p,cnt=0,pro=0,flag=0;
cin >> d;
string s;
cin >> s;
for(int j=0;j<d;j++){
if(s[j]==‘P’){cnt++;}
}
if(cnt100/d>=75){cout<<pro<<endl;}
else{
for(int i=2; i<d-2; i++){
if(cnt
100/d<75){
if((s[i-2]==‘P’||s[i-1]==‘P’)&&(s[i+1]==‘P’||s[i+2]==‘P’)){s[i]=‘P’;cnt++;pro++;}
}
else{flag=1;break;}
}
if(cnt*100/d>=75){flag=1;}
if(flag){cout<<pro<<endl;}
else{cout<<-1<<endl;}
}

}
return 0;

}

could you share the link to question and solution once again

Problem: PROXYC Problem - CodeChef
Solution: CodeChef: Practical coding for everyone

You can provide a proxy only if Chef is absent, which you didn’t check in if condition.
You are modifying s[i]=‘P’ on successful proxy but the question states that Chef needs to be physically present in class when checking if s[i-1] or s[i-2] == ‘P’ and s[i+1] or s[i+2]==‘P’.

Modified your code and it works fine now: CodeChef: Practical coding for everyone

1 Like

I see. Thank you so much

1 Like