Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
12
91
21
power-of-three)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1780: Check if Number is a Sum of Powers of Three
class Solution {
public boolean checkPowersOfThree(int n) {
while (n > 0) {
if (n % 3 > 1) {
return false;
}
n /= 3;
}
return true;
}
}
// Accepted solution for LeetCode #1780: Check if Number is a Sum of Powers of Three
func checkPowersOfThree(n int) bool {
for n > 0 {
if n%3 > 1 {
return false
}
n /= 3
}
return true
}
# Accepted solution for LeetCode #1780: Check if Number is a Sum of Powers of Three
class Solution:
def checkPowersOfThree(self, n: int) -> bool:
while n:
if n % 3 > 1:
return False
n //= 3
return True
// Accepted solution for LeetCode #1780: Check if Number is a Sum of Powers of Three
impl Solution {
pub fn check_powers_of_three(n: i32) -> bool {
let mut n = n;
while n > 0 {
if n % 3 > 1 {
return false;
}
n /= 3;
}
true
}
}
// Accepted solution for LeetCode #1780: Check if Number is a Sum of Powers of Three
function checkPowersOfThree(n: number): boolean {
while (n) {
if (n % 3 > 1) return false;
n = Math.floor(n / 3);
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.