Time exceed error

https://www.codechef.com/viewsolution/45759840
https://www.codechef.com/viewsolution/45648286
I m facing TLE in both situation someone plz help me.

I believe it must be due to the algorithm which u have designed in Python.
Rather you may try the same algo in a lang. like C or C++, or improvise the algo

But I dont understand c++
https://www.codechef.com/viewsolution/45760906
I worked on algorithms but now Iā€™m getting NZEC Error but it is working fine in IDLE

def solve(L,S):
    total_ones=0; total_years=0
    for i in range(0,L):
        if S[i]=='1': total_ones+=1
        total_years+=1
        if (total_ones/total_years)*100.0 - 50.0 >=0:
            return "YES"
    return "NO"

T=int(input())
while T>0:
    L=int(input())
    S=input()
    print(solve(L,S))
    T-=1

Your code lagged because it counted ones for every substring of S, resulting in O(n^2) complexity.

The trick is to find the percentage of ones at every index and break the loop once the criteria is met. Have a look at solve() above.

1 Like

https://www.codechef.com/viewsolution/45760906
Plz help in figuring this out. I dont know what is NZEC, my programm is perfectly working in IDLE but not in CC.

find() returns the first occurrence of a character in a string or -1 if not found.

If we have a string of only 0s, f_one=0. So, dividing anything by 0 results in runtime error.