Why my program keeps getting Time Limit Exceeded? What are some of the reasons of getting TLE?

To understand Time Limit Exceeded(TLE), understanding how the online judge works will help. The online judge allocates resources like memory and CPU for evaluating every submission. However, to ensure that your submission does not keep running for an infinite time, the online judge has to stop your submission from running after a particular time period. This time period is actually decided by the problem setter and is given as one of the inputs to the online judge. Once the submission program runs for time period the judge system issues a system kill command to the program execution and assigns TLE result to the submission.

The most common reason that you would get a TLE is because your program is too slow. If a problem tells you that N <= 100000, and your program has nested loops each which go up to N, your program will never be fast enough. Read the bounds in the input carefully before writing your program, and try to figure out which inputs will cause your program to run the slowest.

The second most common cause of TLE is that your method of reading input and writing output is too slow. In Java, do not use a Scanner; use a BufferedReader instead. In C++, do not use cin/cout - use scanf and printf instead.

In Python, you could try speeding up your solutions by adding the following two lines to the start of your file:

```
import psyco
psyco.full()
```

To see if your method of reading input is fast enough, try solving the Enormous Input Test problem. If you get time limit exceeded, try another method of reading input.

Finally, you may have tested your code on all sorts of large inputs and are sure your code will run inside the time limit. However, CodeChef’s judge may be slower than your computer. It is common for a program to take 2-3 times as long on CodeChef as it does on your computer. The time limits are all attainable(tested by our problem tester), so you will just need to come up with a way of making your algorithm

#include

using namespace std;

int z(int n)

{

int f=5;

int a[100000];

int i=0,c=0;

while((n/5)>0)

{

a[i]=n/f;

c=c+a[i];

i++;

f=f*5;

}

```
return c;
```

}

int main() {

int t,n,i;

cin>>t;

int k[t];

for(i=0;i<t;i++)

{

cin>>n;

k[i]=z(n);

}

for(i=0;i<t;i++)

{

cout<<"\n"<<k[i];

}

return 0;

}

TLE error in the code…how to solve?

It means the program you are using is doing more computations than expected. Usually, loops that goes upto 10^6-10^7 are acceptable with 1-2s time limit. It usually depends on the problem.

Implementation of wrong or way of use(algorithm) data structure .

can anybody tell me what is the problem in my code i am getting TLE erroe when submitted.

http://code.geeksforgeeks.org/eNfy3Q

@dragonemperor @betlista @admin how to input a string whose number of characters we do not know using scanf in c++ ??

1 . High Complexity.

2. Sometimes due to IO functions.

Try to use Fast IO or optimize your code as much as possible.

#include<stdio.h>

long int prod(long int ar[],long int x){

long int p,s=0;

if(x==1)return ar[0];

else if(x==2)return ar[1]*ar[0]+ar[1]+ar[0];
else { for(p=2;p<x;p++)s=ar[p]*(1+prod(ar,x-1))+prod(ar,x-1);}

return s;

}

int magic(long int n){

long int c=1,nn,i=0,p,q=n,j,k,s=0,ar[10000];

nn=0;p=n;

while(p>0){p=p/10;nn++;}

long int a[nn];while(n>0){a[i]=n%10;i++;n=n/10;}

s=prod(a,nn);

if(s%2==0)return 1;

else return 0;

}

int main(){

long int n,c,i,t,j,k;

scanf("%ld",&t);

for(j=0;j<t;j++){k=1;

scanf("%ld",&i);n=0;

while(k<=i){

if(magic(n)==1)k++;n++;

}

```
printf("%ld\n",n-1);
```

}

}

//This is my code for chef hiring problem(code-CHEARMY).why am i getting TLE in codechef? but it is running successfully in my computer.

how to avoid loop that goes upto 10^6 or 10^12?

what is the alternative for it?

#include<stdio.h>

int main()

{

long int a[10000001],n,t,i,j;

scanf("%d",&n);

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

for(i=0;i<n;i++)

{

for(j=0;j<(n-1);j++)

{

if(a[j]>a[j+1])

{

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}

}

}

for(i=0;i<n;i++)

{

printf("%d",a[i]);

}

return 0;

}

`Can u please tell what's wrong in this I am getting time limit error. .`

guys, I have got 1 karma. please increase my karma(by liking) to 3,so that i could ask my doubts in the forum. Thanks in advance for spending your time.

Your program may be so long that it takes more time in compiling than specified in question. Continously calling big loops are example of that.So think big and make your code small.Thank you

Hey I am new to CodeChef and I am not comfortable with this time limit for execution. My all algorithms are logically correct and my program is working perfectly fine on my system but on codechef it is showing TLE error.

Program name is Chef And Keyboard(CHEFKEY):- https://www.codechef.com/problems/CHEFKEY

My solution:- https://github.com/ishaan123/CodeChef/blob/master/codechef-2.cpp

#include <stdio.h>

int main()

{

int i,t,c=d=e=f=0,g,h,N,X[N],Y[N];

scanf("%d",&t);

```
for(i=0;i<t;i++)
{
scanf("\n %d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&X[i]);
}
for(i=0;i<N;i++)
{
scanf("%d",&Y[i]);
}
for(i=0;i<N;i++)
{
if(i%2==0)
{
c=c+X[i];
e=e+Y[i];
}
else{
d=d+X[i];
f=f+Y[i];
}
}
g=c+f;
h=d+e;
if(g<h){
printf("%d",g);
}
else
{
printf("%d \n",h);
}
}
return 0;
}
```

why do i get a timi limit error??

#include<stdio.h>

int main()

{

int t,m[10],n[10],i,j,primes[10^7+1];

for(i=0;i<=10^7;i++)

primes[i]=1;

primes[0]=0;

primes[1]=0;

for(i=2;i*i<=10^7;i++)
{
if(primes[i]==1)
{
for(j=2;i*j<=10^7;j++)

primes[i*j]=0;

}

}

scanf("%d",&t);

for(i=1;i<=t;i++)

scanf("%d%d",&m[i],&n[i]);

for(i=1;i<=t;i++)

{

for(j=m[i];j<=n[i];j++)

{

if(primes[j]==1)

printf("%d\n",j);

}

printf("\n");

}

return 0;

}

y i m getting TLE in my code can anyone help

```
[1]
please help
[Problem set][2]
[1]: https://www.codechef.com/viewsolution/17096612
[2]: https://www.codechef.com/FLMOCK02/problems/STACKS/
```

From the above answer it should be clear to everyone, that when you got time for example 1.01s when timelimit was 1 seconds it doesn’t mean your program finished in 1.01 but was killed = judge is not waiting.

I don’t know which problem you are trying to solve. Please provide problem code and a link to your solution. Don’t post code here. It’s unreadable.

Try removing cin and cout and use printf and scanf instead. This might work. Otherwise your code might have higher complexity. You need to optimize a bit

Hey @rajvir007 here is the solution to your question which I posted in discuss as a new question

https://discuss.codechef.com/questions/80575/input-string-in-c

More over I have upvoted your question so that you can now ask question on your own and upvote and participate in codechef discuss and community rather that posting questions in answer column of editorials.