Power of Four

Easy Topics Company Tags

Return true if the integer n equals 4 raised to a nonnegative integer exponent, and false otherwise. In particular, 1 = 4^0 is a power of four.

Follow-up: Can you decide this without loops or recursion?

Example 1:

Input: n = 64

Output: true

Explanation: Multiplying three copies of 4 gives 4 * 4 * 4 = 64.

Example 2:

Input: n = 8

Output: false

Explanation: The consecutive powers 4^1 = 4 and 4^2 = 16 lie on either side of 8, so no integer exponent produces 8.

Example 3:

Input: n = 0

Output: false

Explanation: Every nonnegative integer power of 4 is positive, so 0 is not a power of 4.

Constraints:

  • -2147483648 <= n <= 2147483647


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.