# (1.00000, -2147483648) test case failed for one code, passed for another code

• I have two sets of codes : (1) passed for all. (2) failed for corner case (1.00000, -2147483648). I understand for (2) -2147483648 taking off negative is 2147483648, exceeding INT_MAX. I don't understand why this case does not fail for (1).

``````(1)
double power (double x, int n) {
if (n == 0)
return 1;
double v = power(x, n / 2);
if (n % 2 == 0) {
return v * v;
} else {
return v * v * x;
}
}

double pow(double x, int n) {
if (n < 0) {
return 1 / power(x, -n);
} else {
return power(x, n);
}
}

(2)

double pow(double x, int n) {
if(x==0 && n==0) return (0);
if(n == 0&&x!=0) return 1;
if (n < 0)
return 1.0/pow(x, -n);
else
{

double v = pow(x, n/2);
if (n%2 == 0)
return v*v;
else
return v*v*x;
}``````

• The reason is if `n= -2147483648`then `-n` will be `-2147483648`. So in 2nd case `if (n < 0) return 1.0/pow(x, -n);` will be always true and hence it will give you stack overflow error. But in first case `power (x,-2147483648)` will call `power (x,-1073741824)` which subsequently will call `power(x, -1)` which will call `power(x,0)`. And `power(x,0)` will `return 1` then `power(x,-1)` will `return 1` subsequently `power(x,-2147483648)` will `return 1`.

The reason for `-(INT_MIN)=INT_MIN` is can be explained as follows `-(INT_MIN)=-(-INT_MAX-1)` which will give you `-(INT_MIN)=INT_MAX+1`. `INT_MAX+1` will wrap around and it will give you again `INT_MIN`

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.