CSES - missing number problem. given range for n is 2<n<10^5. using long long for n works for all test cases. but when using int for n, 5 test cases failed. I tested the n=50000 case.
Using long long
#include <iostream>
using namespace std;
int main() {
freopen("test_input.txt", "r", stdin);
long long sum = 0;
long long n;
cin >> n;
long long tri = ((n+1)*n)/2;
n--;
while(n--){
int x;
cin >> x;
sum += x;
}
cout << tri - sum << endl;
}
output >> 7626
Using int
#include <iostream>
using namespace std;
int main() {
freopen("test_input.txt", "r", stdin);
long long sum = 0;
int n;
cin >> n;
long long tri = ((n+1)*n)/2;
n--;
while(n--){
int x;
cin >> x;
sum += x;
}
cout << tri - sum << endl;
}
output >> -2147476022
I still don’t understand why n needs to be long long? As far as I know, In my code I didn’t overflow the n value. n value is updated only in while loop to take n inputs.