LeetCode Entry

326. Power of Three

13.08.2025 easy 2025 kotlin rust

Is n power of 3?

326. Power of Three easy blog post substack youtube 1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1079

Problem TLDR

Is n power of 3? #easy #math

Intuition

Almost failed at corner case Int.MAX_VALUE, be aware of Int overflow when *3. Another interesting facts:

  • 3-base representation has only single 1, others 0
  • 3^19 is max fit into Int, if 3^19 % n == 0, n is a power of 3

Approach

  • try to write all the different ways
  • the / solution is the most robust
  • some arithmetic fact: log_3(n) = log_x(n)/log_x(3) for any x

Complexity

  • Time complexity: \(O(log(n))\)

  • Space complexity: \(O(1)\)

Code


// 27ms
    fun isPowerOfThree(n: Int) =
        n.toString(3).replace("0", "") == "1"


// 10ms
    fun isPowerOfThree(n: Int) = n > 0 &&
        3.0.pow(1.0*log(1.0*n, 3.0).roundToInt()) == 1.0*n


// 9ms
    fun isPowerOfThree(n: Int) =
        n > 0 && 3.0.pow(19.0).toInt() % n < 1


// 8ms
    fun isPowerOfThree(n: Int): Boolean {
        if (n <= 0) return false
        var x = 1L; val n = 1L * n
        while (x < n) x *= 3
        return x == n
    }


// 8ms
    fun isPowerOfThree(n: Int): Boolean {
        var n = n; if (n > 1) while (n % 3 == 0) n /= 3
        return n == 1
    }


// 0ms
    pub fn is_power_of_three(n: i32) -> bool {
        n > 0 && 3i32.pow(19) % n < 1
    }


// 3ms
    bool isPowerOfThree(int n) {
       while (n > 1 && n % 3 == 0) n /= 3;
       return n == 1;
    }


// 6ms
    def isPowerOfThree(self, n: int) -> bool:
        return n > 0 and 3**19%n<1