Power of Three - Problem

Given an integer n, return true if it is a power of three. Otherwise, return false.

An integer n is a power of three, if there exists an integer x such that n == 3^x.

Input & Output

Example 1 — Perfect Power of Three
$ Input: n = 27
Output: true
💡 Note: 27 = 3³, so it is a power of three
Example 2 — Not a Power of Three
$ Input: n = 0
Output: false
💡 Note: 0 is not a power of three (powers of 3 are positive)
Example 3 — Base Case
$ Input: n = 1
Output: true
💡 Note: 1 = 3⁰, so it is a power of three

Constraints

  • -2³¹ ≤ n ≤ 2³¹ - 1

Visualization

Tap to expand
Power of Three Problem OverviewInput: 27Test numberGivenCheck if 27 = 3^x3⁰ = 13¹ = 33² = 9Powers3³ = 27 ✓Found matchResulttrue27 is power of 3Output
Understanding the Visualization
1
Input
Given integer n to check
2
Process
Apply mathematical test for power of 3
3
Output
Return true if n = 3^x for some integer x
Key Takeaway
🎯 Key Insight: Use mathematical properties like integer limitations or logarithms for efficient checking
Asked in
Google 15 Apple 8 Microsoft 12
180.0K Views
Medium Frequency
~15 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen