how to avoid tle in c++

C++
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.
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 slower. 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 solved 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.
Source

Also in C++: