CodePudding user response:
You get signed integer overflow in sum = (r*(r 1))/2 - ((l-1)*l)/2;.
Change the input parameters to your function to long ints too:
int solve(long l, long r) {
You could also try replacing all your ints and longs with intmax_t (you must #include <stdint.h> first). If that's not enough, you must find an alternative way to solve the problem.

