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
falsefor:n ≤ 0- Non-integer inputs
Function Signature
function isPowerOfFour(n) { // Your code here }
Test Cases
Base cases:
n = 1n = 4n = 0
Powers of 4:
n = 16n = 64n = 256n = 1024
Non-powers:
n = 2n = 8n = 32n = 100
Edge cases:
n = 0n = -4n = -1
Large numbers:
n = 4^15n = 4^16 - 1
Companies:
google
meta
microsoft
amazon
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
