Power of four

August 22, 2022

math-and-geometry

Problem URL: Power of four

We will devide the number by 4 until it's 1. If in any step, we have any reminder, we will return false, otherwise return true.

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        if n == 1:
            return True
        if n < 4:
            return False
        while n > 1:
            if n % 4 ==0:
                n = n/4
            else:
                return False
        return True

Time Complexity: O(log(n))
Space Complexity: O(1)