How to overcome Time Limit Exceed(TLE)?

Many programmers always argue that the problems in Competitive Programming always end up with TLE(Time Limit Exceed). The Main problem of this error is that it will not allow you to know that your solution would reach to correct solution or not!

Why TLE comes?

  • Online Judge Restrictions: TLE comes because the Online judge have some restriction that it will not allow to process the instruction after certain Time limit given by Problem setter usually (1 sec).
  • Server Configuration: The exact time taken by the code depends on the speed of server,architecture of server,OS and certainly on the complexity of the algorithm. So different servers like practice, codechef,  SPOJ etc may have different execution speed. By estimating the maximum value of N (N is total number of instructions of your whole code), you can roughy estimate the TLE would occur or not in 1 sec.
    MAX value of N                       Time complexity
       10^8                              O(N) Border case
       10^7				     O(N) Might be accepted
       10^6                              O(N) Perfect
       10^5                              O(N * logN)
       10^3                              O(N ^ 2)
       10^2                              O(N ^ 3)
       10^9                              O(logN) or Sqrt(N)

    So after analyzing this chart you can roughly estimate your Time complexity and make your code within the upper bound limit.

  • Method of reading input and writing output is too slow: Sometimes methods used by a programmer for input output may cause TLE.

OverCome Time Limit Errors

  • Change methods of Input-Output: You must choose proper input-output functions and data structure which would  help you in optimization.
    • In C++, do not use cin/cout – use scanf and printf instead.
    • In Java, do not use a Scanner – use a BufferedReader instead.
    • In Python, you could try speeding up your solutions by adding the following two lines to the start of your file:
      import psyco
  • Bounds of loops may be reduced :  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. For example if a problem tells you that N <= 100000 or N<=1000000, and your program has nested loops each which go up to N, your program will never be fast enough.
  • Optimize your Algorithm: If nothing works after all this, then you should try changing the algorithm or the approach you are using to solve your problem. Generally , the internal test cases are designed in such a way that you will be able to clear all of them only if you choose the best possible algorithm.
  • Look for Suggestions given: Although this should be the last step but you must look at the comments given below any problem in which other programmers might have given a hint on how the problem can be soved in a better and more efficient way. And even when you overcome TLE try more exhaustive and corner test cases against  your program to check the performance.

Ultimately, with experience you’ll surely come to know what to do and what not to avoid TLEs. The more you code the more you get to know about how to compete TLE.

Practice Now

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Writing code in comment? Please use, generate link and share the link here.