Watch 10 video solutions for Check if Number is a Sum of Powers of Three, a medium level problem involving Math. This walkthrough by NeetCodeIO has 11,101 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false.
An integer y is a power of three if there exists an integer x such that y == 3x.
Example 1:
Input: n = 12 Output: true Explanation: 12 = 31 + 32
Example 2:
Input: n = 91 Output: true Explanation: 91 = 30 + 32 + 34
Example 3:
Input: n = 21 Output: false
Constraints:
1 <= n <= 107Problem Overview: You receive an integer n. The task is to determine whether it can be represented as the sum of distinct powers of three (for example 1, 3, 9, 27...). Each power can be used at most once. The problem reduces to checking whether a combination of these powers adds exactly to n.
Approach 1: Greedy / Base-3 Representation (Time: O(log3 n), Space: O(1))
The key observation: powers of three behave like digits in base-3 representation. If n can be written as a sum of distinct powers of three, then its base-3 representation must contain only digits 0 or 1. A digit 2 would mean the same power of three is used twice, which violates the constraint. Convert the number by repeatedly dividing n by 3 and checking the remainder. If any remainder equals 2, return false. Otherwise continue until n becomes zero. This greedy observation avoids generating combinations entirely and runs in logarithmic time. The approach relies purely on number properties from math and is the optimal solution expected in interviews.
Approach 2: Backtracking Over Powers (Time: O(2k), Space: O(k))
Another way to think about the problem is as a subset-sum over powers of three. First generate powers of three up to n (for example 1, 3, 9, 27...). Then recursively try including or excluding each power while subtracting from the remaining target. If the remaining value reaches 0, a valid combination exists. If it becomes negative or you exhaust all powers, that branch fails. This approach uses recursion and explores the decision tree of subsets, which is why the complexity becomes exponential in the number of powers (k ≈ log3 n). It demonstrates the brute-force reasoning behind the problem and uses techniques from backtracking and recursion.
Recommended for interviews: The greedy base-3 check is the expected solution. Interviewers want to see the insight that the sum of distinct powers of three corresponds directly to base-3 digits restricted to 0 and 1. Implementing the greedy division loop shows strong mathematical reasoning and yields O(log n) time with constant space. The backtracking approach is still useful to discuss because it shows how the problem maps to subset selection before the mathematical shortcut is recognized.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy / Base-3 Representation | O(log n) | O(1) | Best general solution. Uses number properties to check digits in base-3. |
| Backtracking Over Powers | O(2^k), k ≈ log3(n) | O(k) | Useful for understanding the subset-sum formulation or during initial brute-force reasoning. |