INTEST ->> time limit exceeded ----- how to shorten time duration taken to run this program?

//Problem code :: INTEST

#include<iostream>
using namespace std;

int main()

{

	int a,i=0,sum=0;
    
	long k,temp;
    
	cin>>a>>k;
    
	while(i<a)
    
	{
    
		cin>>temp;
    
		if(temp%k==0)
    
		sum=sum+1;
    
                i++;
    
	}
    
	cout<<sum << endl;
}

Dont use cin/cout, use scanf/printf, still that’s just one solution to pass the problem just in time,if you don’t wanna use fast i/o

1 Like

Hello,

You were missing an end of line after cout << sum;, which I have added.

Then, as already suggested, you can either replace cin/cout with scanf/printf, or, as an alternative, you can turn off the automatic synchronization that cin/cout do with stdio, in order to gain more time.

As such, the code below (which is basically your code with the endl added and with the detail above added), gets AC with 4.25 sec:

#include<iostream>
using namespace std;

int main()

{

	ios_base::sync_with_stdio(false);  //this was added
	int a,i=0,sum=0;
    
	long k,temp;
    
	cin>>a>>k;
    
	while(i<a)
    
	{
    
		cin>>temp;
    
		if(temp%k==0)
    
		sum=sum+1;
    
                i++;
    
	}
    
	cout<<sum << endl; //an end of line was added here as well
}  

Best regards,

Bruno

1 Like

use scanf/printf they r almost 5 times faster than cin/cout

1 Like

Thank you!! Bruno and yash!!

ios_base::sync_with_stdio(false)
What does this do?

you can read more here: std::ios_base::sync_with_stdio - cppreference.com

Note, in particular: “If the synchronization is turned off, the C++ standard streams are allowed to buffer their I/O independently, which may be considerably faster in some cases.”

:slight_smile:

1 Like

ok…thanx man, for all the help!!

1 Like

No problem, that’s why we are here :wink:

1 Like