class Solution {
public:
bool isPowerOfFour(int n) {
if(n==1)
return true;
if(n<=0)
return false;
if((n&(n-1)==0)&&((n-1)%3)==0){
return true;
}
return false;
}
};
why this returns false on input n=16?
class Solution {
public:
bool isPowerOfFour(int n) {
if(n==1)
return true;
if(n<=0)
return false;
if((n&(n-1)==0)&&((n-1)%3)==0){
return true;
}
return false;
}
};
why this returns false on input n=16?
Are you sure with the logic you have written? It doesn’t look convincing to me.
My style of writing:
bool isPowerOfFour(int n) {
while(n%4 == 0)
n /= 4;
return (n == 1);
}
Time Complexity: O(log_{4} N)
You are Probably missing set of Opening and Closing pairs “()”.
if(((n&(n-1))==0)&&(((n-1)%3)==0))
This returned true for n = 16.
The question was to do it without loops/recursion and after some searcing on gfg i figured out this method.