Logo
    Akhil Abraham
    Akhil Abraham
    0231 - Power of Two
    0231 - Power of Two

    0231 - Power of Two

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/power-of-two/

    Solution 1 - Iterative Division

    Repeatedly divide n by 2 while it is even. If we end up with 1, then n is a power of two. If n is less than or equal to 0, it cannot be a power of two.

    TimeComplexity:O(log⁡n)TimeComplexity: O(\log n)TimeComplexity:O(logn)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Bit Manipulation

    A power of two in binary has exactly one bit set (e.g. 1, 10, 100). The expression n & (n - 1) clears the lowest set bit. If n is a power of two, the result is 0.

    TimeComplexity:O(1)TimeComplexity: O(1)TimeComplexity:O(1)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    """ 0231.1 - Solution 1 - Iterative Division """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isPowerOfTwo(self, n: int) -> bool:
            """Power of Two Function"""
            if n <= 0:
                return False
            while n % 2 == 0:
                n //= 2
            return n == 1
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isPowerOfTwo(1))    # True  (2^0)
        print(Solution().isPowerOfTwo(16))   # True  (2^4)
        print(Solution().isPowerOfTwo(3))    # False
        print(Solution().isPowerOfTwo(0))    # False
        print(Solution().isPowerOfTwo(-4))   # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0231.2 - Solution 2 - Bit Manipulation """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def isPowerOfTwo(self, n: int) -> bool:
            """Power of Two Function"""
            return n > 0 and (n & (n - 1)) == 0
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().isPowerOfTwo(1))    # True  (2^0)
        print(Solution().isPowerOfTwo(16))   # True  (2^4)
        print(Solution().isPowerOfTwo(3))    # False
        print(Solution().isPowerOfTwo(0))    # False
        print(Solution().isPowerOfTwo(-4))   # False
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()