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 <= 107
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The core idea of a brute force approach for this problem is to explore all possible combinations of powers of three. We repeatedly subtract powers of three from the given number, seeing if we can eventually reach zero. This involves trying every combination, even if it seems inefficient.
Here's how the algorithm would work step-by-step:
def check_if_number_is_sum_of_powers_of_three(number_to_check):
powers_of_three = []
power = 1
while power <= number_to_check:
powers_of_three.append(power)
power *= 3
powers_of_three.reverse()
def can_be_expressed(remaining_number, index):
if remaining_number == 0:
return True
if index == len(powers_of_three):
return False
# Try subtracting the current power of three
if remaining_number >= powers_of_three[index]:
if can_be_expressed(remaining_number - powers_of_three[index], index + 1):
return True
#If subtracting doesn't work, try not subtracting.
# Skip the current power of three
return can_be_expressed(remaining_number, index + 1)
#Start the recursive calls.
return can_be_expressed(number_to_check, 0)
The problem asks if a number can be expressed as the sum of powers of three. The optimal solution involves repeatedly dividing the number by three and checking the remainders. This approach leverages the unique properties of base-3 representation to efficiently determine if the number meets the given condition.
Here's how the algorithm would work step-by-step:
def is_sum_of_powers_of_three(number):
while number > 0:
remainder = number % 3
# If remainder is 2, it can't be a sum of powers of three
if remainder == 2:
return False
number //= 3
# If the number becomes 0, it is a sum of powers of three
return True
Case | How to Handle |
---|---|
Input n is zero. | Return true, as zero can be expressed as an empty sum of powers of three. |
Input n is a negative number. | Return false, as powers of three are always positive. |
Input n is one (3^0). | Return true, as 1 is a power of three. |
Input n is a large number that could lead to integer overflow if not handled carefully (e.g., near the maximum integer value). | The algorithm itself will not cause overflow as long as integer division is used and powers of 3 are less than the maximum integer value. |
Input n is a power of three (e.g., 3, 9, 27). | Return true, as these directly satisfy the condition. |
Input n is the sum of several different powers of three (e.g., 1+3+9=13). | The algorithm should correctly identify and return true. |
Input n cannot be expressed as the sum of distinct powers of three (e.g., 2, 5, 8). | The algorithm should correctly identify and return false. |
Input n requiring many iterations/divisions to determine if it's a sum of distinct powers of 3. | The algorithm's time complexity is logarithmic, so it handles even relatively large inputs efficiently as long as integer overflow isn't a concern. |