# Chef and Proxy Problem Code: PROXYC

Hi Chefs,
What’s wrong with the following code :

for _ in range(int(input())):
d = int(input())
s = input()
cnt = 0
pCnt = s.count('P')
for i in range(2,d-1):
if (pCnt/d>0.75):
break
if s[i] == 'A' and ((s[i-1] == 'P' or s[i-2] == 'P') and (s[i+1] == 'P' or s[i+2] == 'P')) :
cnt += 1
pCnt += 1
if (pCnt/d<0.75):
print(-1)
else :
print(cnt)

#output : NZEC error

1 Like

there is a problem in your range function make it d-2
and also you need to make pCnt/d>0.75 to pCnt/d>=0.75

Sol

for _ in range(int(input())):
d = int(input())
s = input()
cnt = 0
pCnt = s.count(‘P’)
for i in range(2,d-2):
if (pCnt/d>=0.75):
break
if s[i] == ‘A’ and ((s[i-1] == ‘P’ or s[i-2] == ‘P’) and (s[i+1] == ‘P’ or s[i+2] == ‘P’)) :
cnt += 1
pCnt += 1
if (pCnt/d<0.75):
print(-1)
else :
print(cnt)

1 Like

Then also I am getting same nzec error
AFAIK range(1,n) means : it will loop from index zero to index n-1 not till index n right???

In that case, as per the chef and proxy problem’s statement it should not include first 2 indexes and last 2 indexes right??? So last 2 indexes will be d-1 and d as per the problem!!!

@knakul853, I understood the range, it should be d-2 only.
Is my logic wrong? I am getting wrong answer error

your logic is fine but you missed here pCnt/d>0.75 this sould be pCnt/d>=0.75

1 Like

Bro, though m still not having that much coder kind of profile. But I would suggest, that try to reconsider ur logic using ceiling function. See you dont need to deal with decimals then…

1 Like

@knakul853, sorry I missed it in your first reply. Thanks it worked with “>=”. Thanks for the help man
@vinc_k thanks bro even ceiling function is working fine.

Could you help me with this i am also getting WA

t=int(input())
for j in range(t):
d=int(input())
s=input()
c=0
n=0
for i in s:
if i==“P”:
c=c+1
u= float(c/d)
if u>=0.75:
print(0)
continue
for i in range(2,d-2):
if s[i]==“A” and (s[i-1]==“P” or s[i-2]==“P”)and (s[i+1]==“P” or s[i+2]==“P”):
n=n+1
c=c+n
u= float(c/d)
if(u>=0.75):
print(n)
break
if u<0.75:
print(-1)

Why are you printing “0” by using the the block of code :
if u>=0.75:
print(0)
continue

As per the problem statement you should print either -1 or minimum count of proxy attendance.
You should remove that print(0) line and then again try to modify your logic

I am printing 0 in case if there is no need of proxy

1 Like

Hello @ut3698, one simple correction in the following block of code:

for i in range(2,d-2):
if (s[i]=='A') and (s[i-1]=='P' or s[i-2]=='P')and (s[i+1]=='P' or s[i+2]=='P'):
n=n+1
c=c+1

in the above code it should be c=c+1
but you have specified c=c+n

One more point : after making this modification your code took 0.04 sec for execution because too many if conditions and too many assignments, will you please compare your code with mine (https://www.codechef.com/viewsolution/24904405). My code took 0.03 sec.

Hey sorry for the above point even I am in beginner stage only, I just wanted to tell you that try to minimize the running time and space complexity

No need for sorry and thanks for suggestions.