LeetCode Problem

How to Solve Power of Three

To determine if a number is a power of three, the key is recognizing that powers of three follow a predictable pattern. You can solve this problem using recursion or a mathematical approach based on logarithms, iterating over possible values to check for divisibility by 3.

GhostInterview Help

Need help with Power of Three without spending extra time grinding it?

GhostInterview can read Power of Three from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #326Math plus RecursionReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Math plus Recursion
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To determine if a number is a power of three, the key is recognizing that powers of three follow a predictable pattern. You can solve this problem using recursion or a mathematical approach based on logarithms, iterating over possible values to check for divisibility by 3.

Problem Statement

Given an integer n, return true if it is a power of three. Otherwise, return false. A number is a power of three if there exists an integer x such that n equals 3 raised to the power of x.

For example, 27 is a power of three because 27 equals 3 cubed. However, numbers like 0 or -1 cannot be powers of three, as they do not fit this formula. The solution should efficiently check whether a number meets this condition.

Examples

Example 1

Input: n = 27

Output: true

27 = 33

Example 2

Input: n = 0

Output: false

There is no x where 3x = 0.

Example 3

Input: n = -1

Output: false

There is no x where 3x = (-1).

Constraints

  • -231 <= n <= 231 - 1

Solution Approach

Mathematical Approach

Use logarithms to check if a number is a power of three. Taking the logarithm of n base 3 will return an integer if n is a power of three. However, since logarithmic calculations might involve floating-point precision issues, an alternative check can be using modulo arithmetic.

Recursive Approach

A recursive solution checks if n is divisible by 3. If n equals 1, it’s a power of three. If n is divisible by 3, divide n by 3 and recurse. If any division does not leave an integer, return false.

Iterative Approach

An iterative approach involves continuously dividing the number by 3 until it is less than or equal to 1. If at any point n is not divisible by 3, return false.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the approach used. The logarithmic method operates in constant time, O(1), while the recursive and iterative approaches both have a time complexity of O(log n), as the number is repeatedly divided by 3. The space complexity is O(1) for all approaches, as no extra space is used apart from the input variable.

What Interviewers Usually Probe

  • Evaluate the candidate's understanding of recursion and iteration.
  • Check if the candidate can efficiently apply math concepts to solve the problem.
  • Observe how the candidate handles edge cases like negative numbers and 0.

Common Pitfalls or Variants

Common pitfalls

  • Not handling negative numbers or zero correctly, which can lead to incorrect answers.
  • Overcomplicating the solution with unnecessary computations when a simpler method exists.
  • Failing to optimize for large values of n or introducing unnecessary space complexity.

Follow-up variants

  • Check if n is a power of two instead of three.
  • Check if n is a power of an arbitrary integer (other than 3).
  • Implement the solution in a language with different recursion limits.

How GhostInterview Helps

  • GhostInterview provides step-by-step solutions that explain math and recursion concepts clearly.
  • It highlights common interview mistakes and suggests ways to optimize the solution for interview efficiency.
  • With interactive examples, GhostInterview helps candidates see the effect of various edge cases on the solution.

Topic Pages

FAQ

What is the best approach to solve the Power of Three problem?

The most efficient approach is to use logarithms, but iterative or recursive methods are also valid solutions depending on interview constraints.

How do I handle edge cases in the Power of Three problem?

Ensure that you account for negative numbers, zero, and other non-positive values, which cannot be powers of three.

What is the time complexity of the Power of Three problem?

The time complexity is O(log n) for both recursive and iterative approaches, while the logarithmic method can achieve O(1) time complexity.

Can I use recursion to solve this problem?

Yes, recursion is a valid approach. It checks if n is divisible by 3 and continues to divide it until reaching 1 or an invalid state.

What should I focus on during the interview for the Power of Three problem?

Focus on demonstrating a clear understanding of recursion and math principles, and be mindful of handling edge cases efficiently.

GhostInterview Solver

Need direct help with Power of Three instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Power of Three from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.