Many people use endl
and '\n'
interchangeably without understanding the key difference between the functioning of the two.
Sometimes using endl
in place of '\n'
can end up getting you a TLE which otherwise would have been an AC with the use of '\n'
.
So let's see what the difference is.
'\n'
: It inserts a new line to the output stream.
endl
: It inserts a new line and flushes the output stream.
So eventually
cout<<endl;
is equivalent to using
cout<<'\n'<<flush;
Conclusion
Using '\n'
outperforms endl
in terms of speed (or time) when matter is related to new line only.
Related Problem
The problem which I solved and got TLE issue was Escape from Stones. Go and have a shot at it.