Power of Four (LeetCode 342): Solution and Explanation

Easy
Math
Bit Manipulation
Recursion

Given a 32-bit integer n, return true if n equals 4^x for some integer x, and false otherwise. The follow-up asks for an answer without loops or recursion.

Intuition

Write the powers of four in binary: 1 is 1, 4 is 100, 16 is 10000, 64 is 1000000. Each one is a single 1-bit followed by an even number of 0s. That is because 4^x = 2^(2x), so the 1 sits at bit position 2x.

So the question splits in two. Is n a power of two, meaning it has exactly one 1-bit? And if so, is that bit at an even position?

The first part has a classic trick. Subtracting 1 from n flips its lowest 1-bit to 0 and every 0 below it to 1. ANDing n with n - 1 therefore clears the lowest 1-bit. If nothing is left, n had only one 1-bit.

For the second part, build a mask with 1s at every even position: binary 0101...0101, which is 0x55555555. ANDing n with it keeps the bit only if it was at an even position, so a non-zero result means the power of two is also a power of four.

Approach: Power-of-two test plus even-bit mask

  1. If n <= 0, return false. Every power of four is at least 1.
  2. If (n & (n - 1)) != 0, n has more than one 1-bit. Return false.
  3. Return whether (n & 0x55555555) != 0, which is true exactly when the single 1-bit is at an even position.

Step-by-step example

n = 16
  1. 16 in binary is 10000, a single 1-bit at position 4.
  2. Check 1: 16 is positive, so continue.
  3. Check 2: n - 1 = 15 = 01111. 10000 & 01111 = 0, so 16 is a power of two.
  4. Check 3: the mask 0x55555555 has a 1 at position 4, so 16 & mask = 16, which is not 0.
  5. All three checks pass, so the function returns true. Indeed 16 = 4^2.

Code: Python, Java, C++

1class Solution:
2    def isPowerOfFour(self, n: int) -> bool:
3        if n <= 0:
4            return False
5        if (n & (n - 1)) != 0:
6            return False
7        return (n & 0x55555555) != 0
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Power-of-two test plus even-bit mask O(1)
Power of two and mod 3 O(1)
Logarithms O(1)
Divide by 4 in a loop O(log n)
Time

O(1)

A comparison, a subtraction and two AND operations, independent of n.

Space

O(1)

No extra memory beyond a few temporaries.

Other approaches

Divide by 4 in a loop (time O(log n), space O(1))

While n is positive and divisible by 4, divide it by 4. Return whether you reached 1. Clear and safe, but it loops up to 15 times and does not meet the follow-up.

Power of two and mod 3 (time O(1), space O(1))

4^x - 1 is always divisible by 3, while 2 * 4^x - 1 is not. So a positive power of two is a power of four exactly when n % 3 == 1.

Logarithms (time O(1), space O(1))

Check whether log base 4 of n is an integer. It reads naturally but floating-point rounding makes it fragile near large values.

Common mistakes

  • Skipping the n > 0 check. For n = 0, n & (n - 1) is 0 and the function would wrongly accept it, and INT_MIN also has a single 1-bit.
  • Operator precedence in Python, C++ and Java: n & n - 1 == 0 parses as n & ((n - 1) == 0) in Python. Parenthesize the AND.
  • Using the mask 0xAAAAAAAA, which marks odd positions. That accepts 2, 8 and 32 instead of 4, 16 and 64.
  • Accepting any power of two. 32 passes the single-bit test but its bit is at position 5, which is odd.

Frequently asked questions

Why 0x55555555?

Each hex digit 5 is binary 0101, so the 32-bit value 0x55555555 has 1s at positions 0, 2, 4, ..., 30. Those are exactly the positions where powers of four put their single 1-bit.

Why does n & (n - 1) clear the lowest set bit?

Subtracting 1 borrows from the lowest 1-bit, turning it into 0 and all the 0s below it into 1s. Bits above it do not change. ANDing with the original n keeps the higher bits and zeroes the rest.

What is the largest power of four that fits in a 32-bit int?

4^15 = 1073741824 = 2^30. The next one, 4^16 = 2^32, is far beyond the signed 32-bit limit.