Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Power of Four problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Power of Four problem solution, you have given an integer n, return true if it is a power of four. Otherwise, return false. An integer n is a power of four, if there exists an integer x such that n == 4x.

Leetcode Power of Four problem solution

Leetcode Power of Four problem solution in Python.

def isPowerOfFour(self, num):
    """
    :type num: int
    :rtype: bool
    """
    
    return num>0 and num&(num-1)==0 and len("{0:b}".format(num))%2==1

Power of Four problem solution in Java.

public boolean isPowerOfThree(int n) {
         if(n==0){
            return false;
        }
        while(n!=1){
            if(n%3==0){
                n/=3;
            }
            else{
                break;
            }
        }
        return n==1;
    }

Problem solution in C++.

class Solution {
public:
    bool isPowerOfFour(int num) {
        if(num<=0) return false;
        long product = pow(2,31);
        int n = sqrt(num);
        if(n*n!=num) return false;
        return product%n==0;
    }
};

Problem solution in C.

bool isPowerOfFour(int n)
{
    if(n<=0)
        return false;
    if(n==1)
        return true;
    if( (__builtin_ctz(n)%2==0) && (__builtin_ctz(n)>0) && (__builtin_popcount(n)==1) )
        return true;
    return false;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes