Yes, you need to typecast as well. Or take n to be long long int right from the start.
1 Like
Use long long int result[100001] instead of int result[100001]
1 Like
You are welcome
For n=5, distance is: 5+5+4+3+2+1.
So n*(n+1)/2 + n works fine here.
10^5 * 10^5 = 10^10. So, use long long to avoid overflow.
i also found new formula
1+2+3+…+ n = n(n+1)/2
n + (n * (n+1)/2);
sum formula + n;
after getting lot of tle…
1 Like
I have used n + (n * (n+1)/2 ) and it is stored in long , but still I am getting wrong answer .
Can anybody please explain the reason ?
due to overflow use long long int it will hold upto 10**18