I recently found this site called codechef, where you can submit solutions to problems. I had submitted two answers for a question, one in C and the other in C++. Both codes are almost the same. But when the code I submitted in C was executed in 4.89s, the code I submitted in C++ was timed out (more than 8 seconds). How is this possible? Where does the time go?
The question was:
Input
The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 10^9, each.
Output
Write a single integer to output, denoting how many integers ti are divisible by k.
Example
Input:
7 3
1
51
966369
7
9
999996
11
Output:
4
My code in C:
#include<stdio.h>
int main() {
int n,k,t;
scanf("%d %d",&n,&k);
int i,num=0;
for(i=0;i<n;i++) {
scanf("%d",&t);
if(t%k==0) num++;
}
printf("%d",num);
return 0;
}
My Code in C++:
#include<iostream>
using namespace std;
int main() {
int n, k, t,num=0;
cin>>n>>k;
for(int i=0;i<n;i++) {
cin>>t;
if(t%k==0) num++;
}
cout<<num;
return 0;
}