{x}
blog image

Power of Three

Given an integer n, return true if it is a power of three. Otherwise, return false.

An integer n is a power of three, if there exists an integer x such that n == 3x.

 

Example 1:

Input: n = 27
Output: true
Explanation: 27 = 33

Example 2:

Input: n = 0
Output: false
Explanation: There is no x where 3x = 0.

Example 3:

Input: n = -1
Output: false
Explanation: There is no x where 3x = (-1).

 

Constraints:

  • -231 <= n <= 231 - 1

 

Follow up: Could you solve it without loops/recursion?

Solution Explanation

The problem asks to determine if a given integer n is a power of three. This means checking if there exists an integer x such that n = 3<sup>x</sup>.

Two approaches are presented: an iterative approach and a more efficient mathematical approach.

Approach 1: Iterative Division

This approach repeatedly divides the input number n by 3 until it becomes either 1 (indicating it's a power of three) or less than 1 (indicating it's not).

Algorithm:

  1. Handle trivial cases: If n is less than or equal to 0, it cannot be a power of three, so return false.
  2. Iterative division: While n is greater than 2, repeatedly perform the following:
    • Check if n is divisible by 3 using the modulo operator (%). If the remainder is not 0, it's not a power of three, so return false.
    • Divide n by 3 using integer division (//=, /=).
  3. Check the final result: After the loop, if n is equal to 1, it's a power of three; otherwise, it's not.

Time Complexity: O(log3n) - The number of divisions is proportional to the base-3 logarithm of n. In the worst case, we divide n by 3 until it becomes 1.

Space Complexity: O(1) - Constant extra space is used.

Approach 2: Mathematical Approach (using a largest power of 3)

This approach leverages the fact that the largest 32-bit integer that is a power of 3 is 1162261467 (319). If a number n is a power of 3, then it must be a divisor of this largest power of 3.

Algorithm:

  1. Handle trivial cases: If n is less than or equal to 0, it cannot be a power of three, so return false.
  2. Modulo check: Check if 1162261467 is divisible by n. If it is (the remainder is 0), then n is a power of 3.

Time Complexity: O(1) - The modulo operation takes constant time.

Space Complexity: O(1) - Constant extra space is used.

Code Examples (with explanations)

The code examples provided earlier demonstrate both approaches. Let's break down a few examples:

Python (Iterative Approach):

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        while n > 2:  # Continue as long as n is greater than 2
            if n % 3:  # Check for divisibility by 3
                return False # If not divisible, it's not a power of 3
            n //= 3  # Integer division by 3
        return n == 1 #After loop,if n is 1,it's a power of 3
 

Python (Mathematical Approach):

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        return n > 0 and 1162261467 % n == 0 # Check if n divides the largest 32-bit power of 3

The other languages (Java, C++, Go, JavaScript, TypeScript) follow similar logic for both approaches. The mathematical approach is generally preferred due to its superior time complexity. The iterative approach is conceptually simpler but less efficient for larger inputs.