I've come up with a working code for the above problem, for both positive and negative values of x. And the answers for the majority of situations is correct, however the code fails at a corner case and I can't seem to find what the issue is! What condition am I missing:`
int pow(int x, int n, int d) {
int i=0,rem=1;
long long l;
if(x==0)
{
return 0;
}
if(x==1)
{
return 1;
}
if(n==0)
{
return 1;
}
x=x%d;
if(n%2==0||n==0)
{
l = ((pow(x,n/2,d))*(pow((x),n/2,d)))%d;
}
else
{
l = ((x)*(pow(x,(n-1)/2,d))*(pow((x),(n-1)/2,d)))%d;
}
if(x<0 && n%2!=0)
{
return d+l;
}
else
{
return l;
}
}
` The case at which the code gives wrong ans:
A: 71045970
B : 41535484
C : 64735492
My Output: 12942068
Actual Output:20805472