DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

## 🧠 Solving LeetCode Until I Become Top 1% β€” Day `58`

πŸ”Ή Problem: Power of Three

Difficulty: #Easy
Tags: #Math #Recursion


πŸ“ Problem Summary

Check if a given integer n is a power of three β€” meaning it can be expressed as 3^k for some non-negative integer k.
If yes, return True; otherwise, return False.


🧠 My Thought Process

  • Brute Force Idea:
    Keep dividing n by 3 until we either:

    • reach exactly 1 β†’ True (it’s a power of three), or
    • get a remainder (n % 3 != 0) β†’ False.
  • Optimized Strategy:
    This is already efficient because:

    • Each division by 3 reduces n by a factor of 3.
    • The number of steps is O(log₃ n), which is tiny for any integer in range. Alternatively, I could check using maximum power of three in int range and modulus, but the loop is simpler.
  • Algorithm Used:

    Iterative division + modulus check.


βš™οΈ Code Implementation (Python)

class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        while n != 1:
            if n % 3 != 0 or n == 0:
                return False
            n //= 3
        return True
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(log₃ n) β€” repeatedly dividing by 3.
  • Space: O(1) β€” no extra storage.

🧩 Key Takeaways

  • βœ… Using iterative division is a quick and intuitive way to check powers of a number.
  • πŸ’‘ An alternative approach is using math.log or precomputing the largest power of three in integer range and checking divisibility.
  • πŸ’­ Problems like this also appear for powers of two, powers of four, etc., and the logic is nearly identical.

πŸ” Reflection (Self-Check)

  • [x] Could I solve this without help?
  • [x] Did I write code from scratch?
  • [x] Did I understand why it works?
  • [x] Will I be able to recall this in a week?

πŸ“š Related Problems

  • [[231 Power of Two]]
  • [[342 Power of Four]]

* [[1780 Check if Number is a Sum of Powers of Three]]

πŸš€ Progress Tracker

Metric Value
Day 58
Total Problems Solved 414
Confidence Today πŸ˜ƒ
Leetcode Rating 1572

Top comments (0)