In the following code(using c++4.8.1), the time limit is being exceeded. How can I improve this code? Also why can’t I use the statement “endl;” after “cin>>num” in the for loop. Please answer my query ASAP. #include
using namespace std;
int main()
{
long int n,k,num;
int i,count=0;
cin>>n>>k;
for(i=0;i<n;++i)
{
cin>>num;
if(num%k==0)
count++;
}
cout<<count;
return(0);
}
The second most common cause of TLE is that your method of reading input and writing output is too slow. In Java, do not use a Scanner; use a BufferedReader instead. In C++, do not use cin/cout - use scanf and printf instead.
Thanx mate, your suggestion worked!
Also, what about the “endl” part? It showed some function overloading error when I used “endl” after “cin>>num” in the for loop.