Given an integer n
, return true
if it is a power of four. Otherwise, return false
.
An integer n
is a power of four, if there exists an integer x
such that n == 4^x
.
Example 1:
Input: n = 16
Output: true
Example 2:
Input: n = 5
Output: false
Example 3:
Input: n = 1
Output: true
Constraints:
-231 <= n <= 231 - 1
Follow up: Could you solve it without loops/recursion?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To check if a number is a power of four using a brute force approach, we can simply keep dividing the number by four. If at any point the result is not a whole number, then the original number cannot be a power of four.
Here's how the algorithm would work step-by-step:
def is_power_of_four_brute_force(number):
# Powers of four must be positive numbers
if number <= 0:
return False
# Base Case
if number == 1:
return True
current_number = number
while True:
current_number = current_number / 4.0
# Check if the result of the division is a whole number
if current_number == int(current_number):
# If the whole number is 1, then we have a power of four
if current_number == 1:
return True
else:
# If it is not a whole number, then it is not a power of four
return False
The key to efficiently determining if a number is a power of four is to realize powers of four are also powers of two. We can leverage this, along with a clever bit manipulation trick, to avoid iterative division. This helps determine the solution much faster.
Here's how the algorithm would work step-by-step:
def isPowerOfFour(number):
if number <= 0:
return False
# Power of 4 must also be a power of 2.
is_power_of_two = (number & (number - 1)) == 0
if not is_power_of_two:
return False
# Check if the '1' bit is in an odd position.
# Powers of 4 have '1' at odd bit positions only.
odd_position_check = number & 0x55555555
if odd_position_check:
return True
return False
Case | How to Handle |
---|---|
Input is zero | Return false, as 0 is not a power of 4. |
Input is negative | Return false, as powers of 4 are always positive. |
Input is not an integer | If a float is passed, either cast it to an integer or return false, based on requirements (casting may lead to precision issues). |
Input is 1 | Return true, as 4^0 = 1. |
Integer overflow if checking by repeated multiplication | Use bit manipulation or logarithmic approach to avoid potential overflow. |
Input is a power of 2 but not a power of 4 (e.g., 2, 8) | Check if the number is a power of 2 and then verify if the set bit is at an even position to determine if it is a power of 4. |
Maximum integer value (Integer.MAX_VALUE) | Check if Integer.MAX_VALUE is a power of 4; if not, the algorithm must handle it correctly without overflow or incorrect results. |
Input close to zero but greater than zero (e.g., 0.00000001) | If the input is expected to be an integer, ensure that input checking for non-integer values are performed. |