342. Power of Four - Explanation
Description
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: trueExplanation: Multiplying three copies of 4 gives 4 * 4 * 4 = 64.
Example 2:
Input: n = 8
Output: falseExplanation: 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: falseExplanation: Every nonnegative integer power of 4 is positive, so 0 is not a power of 4.
Constraints:
-2147483648 <= n <= 2147483647
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Bit Manipulation - Understanding binary representation, bitwise AND, and how powers of two appear in binary
- Logarithms - Using logarithms to determine exponents and checking for integer results
- Power of Two Detection - The trick that n & (n-1) == 0 identifies powers of two
1. Recursion
Intuition
A number is a power of four if we can repeatedly divide it by 4 until we reach 1. If at any point the number is not divisible by 4 (or becomes zero or negative), it cannot be a power of four.
This naturally leads to a recursive solution where we reduce the problem size by dividing by 4 at each step.
Algorithm
- If
nequals1, returntrue(4^0 = 1). - If
nis less than or equal to0, ornis not divisible by4, returnfalse. - Recursively check if
n / 4is a power of four.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Iteration
Intuition
The same logic as recursion applies here, but we use a loop instead. We keep dividing n by 4 as long as it remains divisible. If we end up with 1, the original number was a power of four.
Algorithm
- If
nis negative, returnfalse. - While
nis greater than1:- If
nis not divisible by4, returnfalse. - Divide
nby4.
- If
- Return
trueifnequals1,falseotherwise.
Time & Space Complexity
- Time complexity:
- Space complexity:
3. Math
Intuition
If n is a power of 4, then n = 4^k for some integer k. Taking the logarithm base 4 of both sides gives k = log4(n). If this result is an integer, then n is a power of four.
We check if the logarithm yields a whole number by verifying the remainder when divided by 1 is zero.
Algorithm
- If
nis less than or equal to0, returnfalse. - Compute
log(n) / log(4)to get the base-4 logarithm. - Return
trueif this value has no fractional part (i.e., modulo 1 equals0).
Time & Space Complexity
- Time complexity:
- Space complexity:
4. Bit Manipulation
Intuition
Powers of four in binary are 1, 100, 10000, 1000000, etc. They have exactly one set bit, and that bit is always at an even position (0, 2, 4, ...).
We can check all even bit positions (0, 2, 4, ..., 30) and see if n equals exactly one of these values: 1, 4, 16, 64, and so on.
Algorithm
- If
nis negative, returnfalse. - Iterate through even bit positions from
0to30(step by2):- If
nequals1 << i(which is4^(i/2)), returntrue.
- If
- If no match is found, return
false.
Time & Space Complexity
- Time complexity:
- Space complexity:
5. Bit Mask - I
Intuition
A power of four must first be a power of two (exactly one bit set). We verify this using n & (n - 1) == 0. But not all powers of two are powers of four (e.g., 2 and 8 are not).
Powers of four have their single set bit at even positions. The mask 0x55555555 (binary: 01010101...) has 1s at all even positions. ANDing with this mask confirms the bit is at a valid position.
Algorithm
- Check that
nis positive. - Check that
nis a power of two:(n & (n - 1)) == 0. - Check that the set bit is at an even position:
(n & 0x55555555) == n. - Return
trueif all conditions are met.
Time & Space Complexity
- Time complexity:
- Space complexity:
6. Bit Mask - II
Intuition
Powers of four follow a pattern when divided by 3: 4^k mod 3 = 1 for all non-negative k. This is because 4 = 3 + 1, so 4^k = (3+1)^k, which when expanded always leaves remainder 1.
Powers of two that are not powers of four (like 2, 8, 32) give remainder 2 when divided by 3. This gives us a simple way to distinguish between them.
Algorithm
- Check that
nis positive. - Check that
nis a power of two:(n & (n - 1)) == 0. - Check that
n % 3 == 1to confirm it's specifically a power of four. - Return
trueif all conditions are met.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Confusing Power of Two with Power of Four
All powers of four are powers of two, but not vice versa. Checking only (n & (n - 1)) == 0 accepts values like 2, 8, and 32 which are powers of two but not four. You must add an additional check to verify the set bit is at an even position.
Floating-Point Precision Errors with Logarithms
Using log(n) / log(4) can introduce floating-point errors. For example, log(64) / log(4) might return 2.9999999... instead of exactly 3. Comparing with == 0 after modulo may incorrectly reject valid powers of four. Consider rounding or using integer-based approaches instead.
Sign in to join the discussion