Help me in solving ODDSUM problem

My issue

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

int main() {
long long int t;
cin>>t;
while(t–){
long long int n;
cin>>n;
long long int ans;

    ans=(n-2)*(n-1)+1;
    cout<<ans<<endl;
    
}

}

My code

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

int main() {
	long long int t;
	cin>>t;
	while(t--){
	    long long int n;
	    cin>>n;
	    long long int ans;
	    
	    
	    ans=(n-2)*(n-1)+1;
	    cout<<ans<<endl;
	    
	}
	

}

Learning course: 1600 to 1800 difficulty problems
Problem Link: Odd Sum Practice Problem in 1600 to 1800 difficulty problems - CodeChef

Your code is ok. It’s just slow according to the constraints. You are asked to do 10^6 inputs and outputs.

That amount is A HELL. I don’t know if you know, but inputs and outputs in any language are some of the slowest functions ever. They do a lot of things to do something as apparently simple as dealing with buffers.

Now, cin, out and endl are particularly slower than anything because they do lots of things (useful be secure, but a pain in competitive programming, which are controlled sandboxes). No need for all that.

Should you give them up and turn back to scanf and printf? Not really. They are useful and quicker to write. You just have to turn off all those secure buffer stuff. Don’t worry, I promess that this is a normal thing every C++ contestant do.

Use this as a template of ever code:

int main() { 
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);

...

ALSO, use a string “\n” instead of endl, like this:

cout << variable << "\n";