I was solving George And Accommodation and submitted two accepted versions of code with slight difference. Compiler used: GNU C++14
Version A (Time: 15ms, Memory: 4kb)
#include <iostream>
using namespace std;
int main(){
int n = 0, p = 0, q = 0, a = 0;
cin >> n;
while(n--){
cin >> p >> q;
if(q - p >= 2) a++;
}
cout << a;
return 0;
}
Version B (Time: 15ms, Memory: 8kb)
#include <iostream>
using namespace std;
int main(){
int n = 0, p = 0, q = 0, a = 0;
cin >> n;
while(n--){
cin >> p >> q;
if(q - p >= 2) ++a;
}
cout << a;
return 0;
}
I always thought ++a
is faster and always use it in my loops. However, why does it require more memory while time being exactly the same? I know the general difference being one increments earlier, and one increments after.