Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Power of Three problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Power of Three problem solution, You are 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 == 3x.

Leetcode Power of Three problem solution

Leetcode Power of Three problem solution in Python.

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n == 1:
            return True
        elif n < 3:
            return False
        
        if not n%3:
            return self.isPowerOfThree(n/3)
        return False

Power of Three problem solution in Java.

public boolean isPowerOfThree(int n) {
    int c=0;
    boolean x=false;
    if(n!=0){
    while(c==0){
        if(n==1){
            x=true;
            break;
        }
        c=n%3;
        n=n/3;
    }
    }
    return x;
}

Problem solution in C++.

class Solution {
public:
bool isPowerOfThree(int n) {

    if(n==0){
        return false;
    }
    
    while(n!=1){
        
        if(n%3!=0){
            return false;
        }
        
        n=n/3;
        
    }
    
    return true;  
}
};

Problem solution in C.

bool isPowerOfThree(int n) {
    if(n==0)
    {return false;}
    else if(n==1||n==3||n==9)
    {return true;}
    else if(n%9!=0)
    {return false;}
    return isPowerOfThree(n/9);
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes