Skip to content

1780. Check if Number is a Sum of Powers of Three #1389

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to determine if a given integer n can be expressed as the sum of distinct powers of three. This means each power of three can be used at most once in the sum.

Approach

The key insight here is to recognize that if a number can be represented as a sum of distinct powers of three, its ternary (base 3) representation should only contain the digits 0 and 1. If any digit in the ternary representation is 2, it means that power of three is used more than once, which is not allowed. Thus, the solution involves converting the number to its base 3 representation and checking for the presence of the digit 2.

Let's implement this solution in PHP: 1780. Check if Number is a Sum of Powers of Three

Replies: 1 comment 2 replies

Comment options

mah-shamim
Mar 4, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Mar 4, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants