
sumOfThrees
Given a number n
, return a string representing it as a sum of distinct powers of three, or return "Impossible"
if that’s not possible to achieve.
Example:
- For
n="4",
the output should besumOfThrees("4") = "3^1+3^0"
.4
can be represented as3+1
which is in fact3
to the power of1
plus3
to the power of0
- For
n="2",
the output should besumOfThrees("2") = "Impossible"
There is no way to represent2
as a sum of distinct powers of3
.
Input/Output
-
[execution time limit] 0.5 seconds
-
[input] string n
A string representing the number
n
. It is guaranteed that1 ≤ n ≤ 9 × 1016
. -
[output] string
- A string representing the sum of powers of three which adds up to
n
, or"Impossible"
if there is no solution. If the solution does exist, it should be return as"3^a1+3^a2+ ... +3^an"
, whereai
for0 ≤ i ≤ n
represents the corresponding exponent of the term. The terms in the string should also be sorted in descending order, meaning that higher powers should appear before the lower ones in the string ("3^0+3^1"
is incorrect, whereas"3^1+3^0"
is correct).
- A string representing the sum of powers of three which adds up to
Post Comment