Facebook Pixel

Power of Four

JavaScript
easy
10 mins

Given an integer n, return true if it is a power of four. Otherwise, return false.

A number is a power of four if there exists an integer x such that n === 4^x.

Examples

Input: n = 1 Output: true Explanation: 4^0 = 1
Input: n = 16 Output: true Explanation: 4^2 = 16
Input: n = 8 Output: false Explanation: 8 is not a power of 4
Input: n = 0 Output: false Explanation: 0 is not a power of 4

Constraints

  • (-2^{31} \leq n \leq 2^{31} - 1)
  • Return false for:
    • n ≤ 0
    • Non-integer inputs

Function Signature

function isPowerOfFour(n) { // Your code here }

Test Cases

Base cases:

  • n = 1
  • n = 4
  • n = 0

Powers of 4:

  • n = 16
  • n = 64
  • n = 256
  • n = 1024

Non-powers:

  • n = 2
  • n = 8
  • n = 32
  • n = 100

Edge cases:

  • n = 0
  • n = -4
  • n = -1

Large numbers:

  • n = 4^15
  • n = 4^16 - 1

Companies:

google
meta
microsoft
amazon

Solve Similar questions 🔥

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.