math and geometry August 22, 2022

Power of four

Time O(log(n)) Space O(1) Open original problem

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)