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


Why do I get a Time Limit Exceeded?


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 ♦
accept rate: 42%

12next »

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


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


answered 20 Mar '13, 16:26

admin's gravatar image

0★admin ♦♦
accept rate: 37%

edited 30 Apr '14, 08:14


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


answered 30 Mar '16, 19:54

rajvir007's gravatar image

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

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 to solve? :(


answered 25 Oct '15, 13:43

yash2code's gravatar image

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.


answered 26 Oct '15, 23:43

uppermeniscus's gravatar image

accept rate: 0%

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


answered 27 Oct '15, 13:17

darkhire21's gravatar image

accept rate: 8%

Can anybody tell me what is the problem in my code i am getting TLE error in this code when submitted.


answered 29 Oct '15, 18:05

nitinsinghal94's gravatar image

accept rate: 0%

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


answered 29 Oct '15, 18:07

nitinsinghal94's gravatar image

accept rate: 0%

@nitinsinghal94 please if you can tell which problem you are taking about.


answered 29 Oct '15, 19:31

coderji's gravatar image

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.


answered 06 Apr '16, 11:22

geek_geek's gravatar image

accept rate: 16%


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++; }


} } //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.


answered 10 Jun '16, 14:22

sayantan11995's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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:


question asked: 20 Mar '13, 16:22

question was seen: 73,665 times

last updated: 20 Jan, 22:56