Check if Number is a Sum of Powers of Three (LeetCode 1780): Solution and Explanation
Decide whether n can be written as a sum of distinct powers of three. The base-3 representation answers it directly: n works exactly when none of its base-3 digits is 2.
Intuition
Any number can be written in base 3. The digit at position k says how many copies of 3^k the number contains, and that digit is 0, 1 or 2.
A sum of distinct powers of three uses each power zero times or once, so it is a base-3 number made only of 0s and 1s. Because base-3 representation is unique, n is such a sum exactly when its own base-3 digits contain no 2.
The digits come out one at a time from the bottom: n % 3 is the lowest digit, and n // 3 drops it. So the whole check is a short loop that stops at the first 2.
Approach: Read the base-3 digits
- While n is greater than 0, compute n % 3, the digit for the current power of three.
- If the digit is 2, that power would be needed twice, so return false.
- Otherwise set n = n // 3 to move to the next power.
- When n reaches 0 every digit was 0 or 1, so return true.
Step-by-step example
n = 12
- 12 % 3 = 0: the digit for 3^0 = 1 is 0, so 1 is not used. 12 // 3 = 4.
- 4 % 3 = 1: the digit for 3^1 = 3 is 1, so 3 is used once. 4 // 3 = 1.
- 1 % 3 = 1: the digit for 3^2 = 9 is 1, so 9 is used once. 1 // 3 = 0.
- n is 0 and no digit was 2. 12 is 110 in base 3, and 12 = 3 + 9, so return true.
Code: Python, Java, C++
1class Solution:
2 def checkPowersOfThree(self, n: int) -> bool:
3 while n > 0:
4 if n % 3 == 2:
5 return False
6 n //= 3
7 return True1class Solution {
2 public boolean checkPowersOfThree(int n) {
3 while (n > 0) {
4 if (n % 3 == 2) return false;
5 n /= 3;
6 }
7 return true;
8 }
9}1class Solution {
2public:
3 bool checkPowersOfThree(int n) {
4 while (n > 0) {
5 if (n % 3 == 2) return false;
6 n /= 3;
7 }
8 return true;
9 }
10};Time and space complexity
O(log n)
Each iteration divides n by 3, so the loop runs once per base-3 digit, at most 15 times for n up to 10^7.
O(1)
Only n itself is updated.
Other approaches
Greedy from the largest power (time O(log n), space O(1))
Start from the largest power of three not above n and subtract each power that still fits, going down. If n reaches 0 return true. Because every smaller power together sums to less than the next one, this greedy choice is forced, and it ends up reading the same digits from the top.
Backtracking over subsets (time O(2^log n), space O(log n))
Try including or excluding each power of three and check whether any subset sums to n. It is correct but explores up to 2^15 subsets and ignores the structure that makes the answer immediate.
Common mistakes
- Checking only whether n is a power of three. 12 is not a power of three but is 3 + 9.
- Allowing a power to be used twice. 6 = 3 + 3 is not allowed, and its base-3 form 20 has a 2.
- Writing the greedy version and forgetting to stop at the smallest power 3^0 = 1.
Frequently asked questions
Why does a digit 2 rule the number out entirely?
Base-3 representation is unique. If n had a different way to be written as distinct powers, that way would itself be a base-3 representation with only 0 and 1 digits, contradicting the one that has a 2.
Is 1 a valid answer by itself?
Yes. 1 = 3^0, a sum with a single power, so n = 1 returns true.
Does the same trick work for other bases?
Yes. A number is a sum of distinct powers of b exactly when its base-b digits are all 0 or 1. For b = 2 every number qualifies, which is why binary works.