Power of Four
Using Log
bool isPowerOfFour(int n) {
double power = log(n) / log(4);
double e = 1e-9;
if(abs(power - floor(power)) < e)
return true;
else
return false;
}Time Complexity:
Space Complexity:
Using Recursion
bool isPowerOfFour(int n) {
if(n < 1)
return false;
if(n == 1)
return true;
return((n % 4 == 0) && isPowerOfFour(n / 4));
}Time Complexity:
Space Complexity: for the call stack else
Last updated