Skip to main content
LeetCode 1780, Medium. Topics: Math, Recursion, Backtracking. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 48 parametrized pytest cases, and a playground notebook:

Problem

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 == 3^x.

Examples

Constraints

  • 1 <= n <= 10^7

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026