Mark As Completed Discussion

One Pager Cheat Sheet

  • Write a method powerOfThree which determines if a given non zero positive integer num between 1 and 2147483647 is a power of 3, with an expected space complexity of O(logn) and an expected time complexity of O(1).
  • The ^6 in 3^6 stands for the exponent calculated using the formula base^exponent = result.
  • Loops are a useful control structure in programming used to repeat certain code multiple times, saving the developer time when dealing with large data sets or unknown iterations.
  • The while-loop can be considered as a repeating IF-statement because it needs an initial evaluation of the condition before continuing to repeat a certain set of instructions as long as the condition is true.
  • The number 729 can be expressed using exponential notation as 3^6, since it is the result of raising 3 to the 6th power.
  • The exponential value of any number can be calculated without loops, simply by raising that number to the power of itself.
  • We can solve this problem in O(logn) time complexity and O(1) space complexity by continuously dividing the number by 3.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Got more time? Let's keep going.

If you had any problems with this tutorial, check out the main forum thread here.