Power of Four | Practice | GeeksforGeeks

Given a number N, check if N is power of 4 or not.
Example 1:
Input: 
N = 64
Output: 1
Explanation:
43= 64
Example 2:
Input: 
N = 75
Output : 0
Explanation :
75 is not a power of 4.
+1
princejee20191 month ago
C++ || 0ms || Recursive Solution|| Easy to Understand 
    int isPowerOfFour(unsigned int n){     if(n<1)return 0;     if(n==1)return 1;     if(n%4!=0)return 0;     return isPowerOfFour(n/4);   }
    // Like wise you can find any number power ...like power of 3,power of 5,power of any number !!
IT's cool na !!  HAPPY CODING !!
0
aniketag7871 month ago
class Solution{   int isPowerOfFour(long n)   {// Your code herefor(int i = 0; i < 31; i++){        double ans = Math.pow(4,i);        if(ans == n){        return 1;    }}return 0;   }}
+1
princejee20192 months ago
C++ Code || Easy To Understand
 int isPowerOfFour(unsigned int n){      if(n==1){         return 1;     }     int a =1;     for(int i =2;i<16;i++){         if(n==a){             return 1;         }         a =a*4;     }     return 0;   }
0
abhinav72903 months ago
int isPowerOfFour(unsigned int n)   {       long long int a[15];       a[0]=1;       for(int i=1;i<15;i++)       {           a[i] = a[i-1] *4;       }       for(int i=0;i<15;i++)       {           if(a[i]==n)           return 1;       }     return 0;   }
0
sahuprashant294 months ago
int isPowerOfFour(long n)   { if(n==0)return 0;       if((n&(n-1))==0 && (int)(Math.log(n)/Math.log(2))%2==0)return 1;       return 0;    }
+1
skp915137 months ago
 
Time complexity less than o(n)
95 % of the cases we won't go in the while loop.
class Solution{   public:   int isPowerOfFour(unsigned int n)   {     // Your code goes here     if(n==4 || n ==1)     return 1 ;     if(n==2 || n ==3)     return 0 ;     if(n%2!=0)     return 0 ;     if(n%4!=0)     return 0 ;     if(n%8!=0)     return 0 ;      if(n%10 == 4 || n%10 == 6 ){     while(n!=0){         if(n==4)         return 1;         if(n %4 !=0)         return 0 ;                  n = n/4;              }     return 1 ;      }     else return 0 ;        }};
 
-2
amonk7 months ago
int isPowerOfFour(unsigned int n)   {       if(n==0){           return 0;       }       if(floor(log(n)/log(4)) == ceil(log(n)/log(4))){           return 1;       }return 0;   }
0
Sawi Sharma1 year ago
Sawi Sharma
Python3 solusann:
def isPowerofFour(self, n):        # code here        self.temp=1        if n==1:            return 1        while self.temp<=n:            if self.temp==n:                return 1            else:                self.temp*=4        return 0
0
Annanya Mathur1 year ago
Annanya Mathur
int isPowerOfFour(unsigned int n){int z=0,a=0;  if(n==1)  return 1;  while(n)  {      if(n&1)      a++;      else      z++;      n=n>>1;  }  if(a==1 && z%2==0)  return 1;  else  return 0;}
0
Yash Patil2 years ago
Yash Patil
double val=(Math.log10(n)/Math.log10(4));int res=(int)val;if(val==res){    return 1;}
   return 0;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.