Time complexity: O(logn)
Since the problem gets divided into half in each stage of recursion the time complexity is O(logn)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
long long int power(long long int a,long long int n){ | |
long long int halfpow; | |
if(n==0) | |
return (long long int)1; | |
halfpow=power(a,n/2); | |
if(n%2==0){ | |
return halfpow*halfpow; | |
} | |
else{ | |
return a*halfpow*halfpow; | |
} | |
} | |
int main(){ | |
cout<<power(10,10); | |
} | |