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)
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
your AC code
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)
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!!!
Am I correct or anything missing in my understanding level??? Please help me
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…
@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.
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
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 (CodeChef: Practical coding for everyone). 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