The following code is giving an error by saying the message "singed integer overflow". And the error is in this test case :
input : 208170109961052
Output : 4611790103482368430
Expected Output : 104085054980526
The input range is 1 ≤ n ≤ 10^15
#include "bits/stdc++.h"
using namespace std;
int main()
{
long long n;
cin >> n;
if (n % 2)
{
cout << (((n - 1) * (n - 1)) / 4 + (n - 1) / 2) - (((n + 1) / 2) * ((n + 1) / 2)) << '\n';
}
else
{
cout << ((n * n) / 4 + n / 2) - ((n / 2) * (n / 2)) << '\n';
}
return 0;
}
As far as I know long long is the largest datatype. How can I solve this ?