Why do I get time limit exceeded? PROBLEM: Odd Sum

#include
#include
#define ll long long

void FastIO(){
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
}

void solve(){
ll n;
std::cin>>n;
ll result=1+(n-2)*(n-1);
std::cout<<result<<std::endl;
}

int main(){
FastIO();
int t;
std::cin>>t;
while(t–){
solve();
}
return 0;
}

std::cout<<std::endl

is a slow operation as it calls std::cout.flush() implicitly after sending ‘\n’ to the default output stream. This is useful only in interactive problems.

Replace std::endl with ‘\n’, and check if this fixes the TLE issue.

1 Like

You’re right, thank you. So is better to use ‘\n’ in general right?

That’s right, except in interactive problems as I said.

1 Like