Check if number is a sum of powers of three
January 4, 2023
math-and-geometryProblem URL: Check if number is a sum of powers of three
We will divide the number by 3 until it becomes 0. If the remainder is 1, then we add 1 to the result. Otherwise, we add 0 to the result.
class Solution:
def checkPowersOfThree(self, n: int) -> bool:
while n > 0:
if n % 3 == 2:
return False
else:
n //= 3
return True
Time complexity: O(log(n))
Space complexity: O(1)