More content will be added soon , if you have any different or better solution then please post in the comment section of the respective question.
Method 1: brute forceint pow(int x, int y){if(y == 1) return x ;return x * pow(x, y-1) ;}Method 2: Efficient logarithmic time complexitylong pow(long x, long n){long result = 1;while ( n ) {if ( n & 1 ) {result = result * x;n = n-1;}x = x*x;n = n/2;}return result;}
Post a Comment
1 comments:
Method 1: brute force
int pow(int x, int y)
{
if(y == 1) return x ;
return x * pow(x, y-1) ;
}
Method 2: Efficient logarithmic time complexity
long pow(long x, long n)
{
long result = 1;
while ( n ) {
if ( n & 1 ) {
result = result * x;
n = n-1;
}
x = x*x;
n = n/2;
}
return result;
}
Post a Comment