You are not logged in. Please login at www.codechef.com to post your questions!

×

Why do I get a Time Limit Exceeded?

1
1

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

asked 20 Mar '13, 16:22

tojochacko's gravatar image

2★tojochacko ♦
560101924
accept rate: 42%


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

link

answered 20 Mar '13, 16:26

admin's gravatar image

0★admin ♦♦
15.9k347484508
accept rate: 35%

edited 30 Apr '14, 08:14

4

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.

(13 Nov '14, 17:12) betlista ♦♦3★

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

link

answered 30 Mar '16, 19:54

rajvir007's gravatar image

3★rajvir007
1913
accept rate: 0%

edited 30 Mar '16, 19:55

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.

(31 Mar '16, 11:39) jain_mj3★
#include <iostream>
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? :(

link

answered 25 Oct '15, 13:43

yash2code's gravatar image

0★yash2code
1
accept rate: 0%

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

(25 Oct '15, 16:47) dragonemperor3★

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.

link

answered 26 Oct '15, 23:43

uppermeniscus's gravatar image

1★uppermeniscus
212
accept rate: 0%

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

link

answered 27 Oct '15, 13:17

darkhire21's gravatar image

2★darkhire21
5814
accept rate: 8%

can anybody tell me what is the problem in my code i am getting TLE erroe when submitted. http://code.geeksforgeeks.org/eNfy3Q

link

answered 29 Oct '15, 18:07

nitinsinghal94's gravatar image

0★nitinsinghal94
1
accept rate: 0%

1 . High Complexity. 2. Sometimes due to IO functions.

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

link

answered 06 Apr '16, 11:22

geek_geek's gravatar image

4★geek_geek
43811
accept rate: 16%

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.

link

answered 10 Jun '16, 14:22

sayantan11995's gravatar image

2★sayantan11995
1
accept rate: 0%

how to avoid loop that goes upto 10^6 or 10^12? what is the alternative for it?

link

answered 11 Jun '16, 15:49

shukla_pragun's gravatar image

3★shukla_pragun
1
accept rate: 0%

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&lt;n;i++)" {="" for(j="0;j&lt;(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. .
link

answered 02 Jul '16, 19:37

dragon96's gravatar image

1★dragon96
283
accept rate: 20%

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.

link

answered 03 Jul '16, 14:19

vamsi_deva's gravatar image

3★vamsi_deva
4715
accept rate: 0%

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

link

answered 02 Oct '16, 01:14

parth_7897's gravatar image

2★parth_7897
1
accept rate: 0%

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

link

answered 06 Nov '16, 15:28

ishaan102119's gravatar image

2★ishaan102119
1
accept rate: 0%

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??

link

answered 10 Mar, 13:36

vinit_dalmia2's gravatar image

1★vinit_dalmia2
1
accept rate: 0%

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;ii<=10^7;i++) { if(primes[i]==1) { for(j=2;ij<=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; }

link

answered 11 Aug, 12:03

mukhesh123's gravatar image

0★mukhesh123
1
accept rate: 0%

Why do i get time limit error

(11 Aug, 12:04) mukhesh1230★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×580
×188

question asked: 20 Mar '13, 16:22

question was seen: 59,817 times

last updated: 11 Aug, 12:04