Power of Three - Solution & Explanation
Problem Statement
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?
Approach Overview
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.
Approach 1: Iterative Division
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).
Complexity
Time Complexity: O(log n)
Space Complexity: O(1)
Approach 2: Mathematical Check Using Logarithms
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.
Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Approach 3: Trial Division
If n \gt 2, we can continuously divide n by 3. If it's not divisible, it means n is not a power of 3, otherwise we continue dividing by 3 until n is less than or equal to 2. If n equals 1, it means n is a power of 3, otherwise it's not a power of 3.
Time complexity O(log_3n), space complexity O(1).
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
Approach 4: Mathematics
If n is a power of 3, then the maximum value of n is 3^{19} = 1162261467. Therefore, we only need to check if n is a divisor of 3^{19}.
Time complexity O(1), space complexity O(1).
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Iterative Division | Time Complexity: O(log n) |
| Mathematical Check Using Logarithms | Time Complexity: O(1) |
| Trial Division | — |
| Mathematics | — |
Detailed Complexity Analysis
| 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. |
Video Solution
Power of Three | Live Coding with Explanation | Leetcode - 326 • Algorithms Made Easy • 14,889 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Power of Three easy or hard?
Power of Three Python/Java solution
How to solve Power of Three in O(1)?
What is the best approach for Power of Three?
Is Power of Three asked at Google/Amazon/Meta?
What data structure is used in Power of Three?
What is the time complexity of Power of Three?
Ready to solve this problem?
Practice Power of Three with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor