Power of Three (LeetCode 326): Solution and Explanation
Decide whether an integer n equals 3^x for some integer x. Negative numbers and zero never qualify. The follow-up asks for a solution with no loop or recursion.
Intuition
The natural approach is to peel off factors of 3. While n is divisible by 3, divide it. If you end at 1, every factor was a 3 and n is a power of three. If you stop at a remainder, n has some other prime factor. For 45 the chain is 45, 15, 5, and 5 leaves remainder 2, so the answer is false.
To drop the loop, use the fact that the input is a 32-bit integer. The powers of three that fit are 3^0 = 1 up to 3^19 = 1162261467; 3^20 is already larger than 2^31 - 1. So the question becomes: is n one of these 20 numbers?
3 is prime, so the prime factorization of 3^19 is nineteen 3s and nothing else. Any positive divisor of it must be built from some of those 3s, which means it is 3^k for some k from 0 to 19. In the other direction, every power of three in range divides 3^19. So for positive n, 1162261467 % n == 0 is exactly the test we need.
Approach: Divisor of the largest 32-bit power of three
- If
n <= 0, return false, since every power of three is positive. - Take
maxPower = 1162261467, which is 3^19, the largest power of three that fits in a signed 32-bit int. - Return whether
maxPower % n == 0. Only powers of three divide a pure power of a prime.
Step-by-step example
n = 27
- The slow way first: 27 / 3 = 9, 9 / 3 = 3, 3 / 3 = 1, all with remainder 0, so 27 = 3^3.
- In the code, 27 > 0, so the guard passes.
- maxPower = 1162261467 = 3^19.
- 1162261467 % 27 = 0 because 3^19 / 3^3 = 3^16. Return true.
Code: Python, Java, C++
1class Solution:
2 def isPowerOfThree(self, n: int) -> bool:
3 if n <= 0:
4 return False
5 max_power = 1162261467 # 3^19, the largest power of 3 in a 32-bit int
6 return max_power % n == 01class Solution {
2 public boolean isPowerOfThree(int n) {
3 if (n <= 0) return false;
4 int maxPower = 1162261467; // 3^19, the largest power of 3 in a 32-bit int
5 return maxPower % n == 0;
6 }
7}1class Solution {
2public:
3 bool isPowerOfThree(int n) {
4 if (n <= 0) return false;
5 int maxPower = 1162261467; // 3^19, the largest power of 3 in a 32-bit int
6 return maxPower % n == 0;
7 }
8};Time and space complexity
O(1)
One comparison and one modulo, regardless of n.
O(1)
A single constant and no extra storage.
Other approaches
Repeated division (time O(log n), space O(1))
Divide by 3 while the remainder is 0 and check that you end at 1. It is the most readable version and works for any integer width, but it loops up to 19 times and does not meet the follow-up.
Logarithm (time O(1), space O(1))
Compute log(n) / log(3) and test whether it is an integer. It is short, but floating-point error makes values like log(243) / log(3) come out as 4.999999 in some languages, so it needs an epsilon and is fragile.
Precomputed set (time O(1), space O(1))
Store the 20 powers of three in a hash set and look n up. It is O(1) and exact, but the divisor trick does the same job with a single constant.
Common mistakes
- Skipping the
n <= 0guard.1162261467 % 0divides by zero, and negative divisors such as -3 also divide 3^19. - Using the divisor trick for a base that is not prime. It works for 3 because 3 is prime; for a base like 4, 1 is fine but the divisors of 4^k include 2, which is not a power of 4.
- Trusting a floating-point logarithm without a tolerance, which misclassifies some exact powers.
Frequently asked questions
Why 1162261467 specifically?
It is 3^19, the largest power of three below 2^31 - 1 = 2147483647. 3^20 is 3486784401, which does not fit in a signed 32-bit integer.
Does this trick work for Power of Two?
Yes, 2^30 % n == 0 for positive n works because 2 is prime, though the bit trick n & (n - 1) == 0 is more common there. It does not work for Power of Four, because 4 is not prime.
Is 1 a power of three?
Yes. 1 = 3^0, and the code agrees because 1162261467 % 1 is 0.