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 - 1Follow up: Could you solve it without loops/recursion?
Problem Overview: Given an integer n, determine whether it can be written as 3^k for some integer k ≥ 0. In other words, repeatedly multiplying 3 by itself should eventually produce n. If no such exponent exists, the number is not a power of three.
This problem sits in the math category and often appears as a quick test of number properties and integer manipulation. The goal is not brute-force exponent generation but recognizing patterns in powers of three.
Approach 1: Iterative Division (Time: O(log₃ n), Space: O(1))
The simplest way to verify a power of three is repeated division. If n is divisible by 3, divide it by 3 and continue the process. A valid power of three will eventually reduce to exactly 1. If at any step n % 3 != 0, the number contains another prime factor and cannot be expressed as 3^k.
This works because powers of three are composed solely of the factor 3. Every division removes one exponent. The loop runs at most log₃ n times since each step shrinks the value by a factor of three. Memory usage stays constant because only the input variable is modified.
This approach is reliable, easy to implement in any language, and avoids floating‑point precision problems. A recursive version follows the same logic and fits naturally within recursion patterns, but the iterative loop is more efficient in practice.
Approach 2: Mathematical Check Using Logarithms (Time: O(1), Space: O(1))
A number n is a power of three if there exists an integer k such that n = 3^k. Taking logarithms gives k = log(n) / log(3). If the computed value of k is an integer, then n must be a power of three.
The implementation computes k = log10(n) / log10(3) (or using natural logs) and checks whether k is very close to an integer using rounding. This converts the repeated division process into a constant-time mathematical check.
The tradeoff is floating‑point precision. For large values of n, rounding errors can cause near-integer values like 4.999999. Because of this, production code usually compares the difference against a small epsilon or prefers integer division instead.
Recommended for interviews: The iterative division approach is the expected solution. It clearly shows understanding of number factorization and runs in O(log n) time with O(1) space. The logarithm method demonstrates mathematical insight but may introduce precision issues, so interviewers generally favor the division approach for clarity and reliability.
This approach leverages the fact that if a number n is a power of three, it should be divisible by 3 repeatedly until it becomes 1. If at any step, n is not divisible by 3 and it's greater than 1, it cannot be a power of three.
The function checks divisibility by 3 in a loop, dividing n repeatedly by 3 until it is either 1 (return true) or it isn't divisible by 3 anymore (return false).
C++
Java
Python
C#
JavaScript
Time Complexity: O(log n)
Space Complexity: O(1)
This approach uses logarithms to determine if a number is a power of three. By taking the logarithm of the number and comparing it to the logarithm base 3, we can check if they form an integer ratio.
This solution calculates the base 10 logarithm of n and divides it by the base 10 logarithm of 3. If the resulting value is an integer, then n is a power of three.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1)
Space Complexity: O(1)
| Approach | Complexity |
|---|---|
| Iterative Division | Time Complexity: O(log n) |
| Mathematical Check Using Logarithms | Time Complexity: O(1) |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Division | O(log₃ n) | O(1) | Best general solution. Reliable integer arithmetic with no floating‑point issues. |
| Mathematical Logarithm Check | O(1) | O(1) | Useful for quick mathematical validation when floating‑point precision is acceptable. |
Power of Three | Live Coding with Explanation | Leetcode - 326 • Algorithms Made Easy • 13,217 views views
Watch 9 more video solutions →Practice Power of Three with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor