TLE in cpp - endl vs '\n'

This is only for cpp users.
You must have seen that using
ios_base::sync_with_stdio(false); and cin.tie(NULL); decreases the code runtime.

What you may not know is that using “\n” instead of endl also decreases the the runtime.
You can also use printf and scanf instead of using cin cout as they are faster as compared to cin cout to reduce the runtime of the code.

Hope this helps!!

4 Likes

but in cpp, I think it did the same thing:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}

So, what is the necessity of using “/n” instead of endl. @abhaychandna Please explain it bit clearly, I am so noob in C++.

Endl flushes output buffer whereas /n don’t do this.This increase time using endl.
For more detail google it.

2 Likes
2 Likes