LeetCode #342 Easy

Power of Four

Power of Four is LeetCode 342 (Easy). Given an integer n, return true if n = 4^x for some integer x ≥ 0, and false otherwise.

  • n can be zero or negative, and neither is ever 4^x.
  • n fits in a 32-bit signed int, so the largest value that must pass is 4^15 = 2^30.
  • Follow-up: answer without loops or recursion.
Constraints
  • -2³¹ <= n <= 2³¹ - 1
mathbit-manipulationrecursion
Open on LeetCode ↗
02

Intuition

Every power of four is also a power of two, because 4^x = 2^(2x). So a power of four has exactly one set bit, and that bit sits at an even position (bit 0, 2, 4, …, 30).

  • One set bit: n & (n - 1) == 0 checks this, the same test used in Power of Two.
  • Even position: the mask 0x55555555 is 0101…0101 in binary, a 1 at every even position. ANDing the lone bit with it gives a non-zero result only when the bit is at an even position.

Powers of two that are not powers of four (2, 8, 32, …) put their bit at an odd position, so the mask removes it.

03

Approach

1

Reject n ≤ 0

A power of four is always positive, so return false for zero and negative numbers first. This also keeps 0 from slipping through the bit test, since 0 & -1 is 0.

2

Check for exactly one set bit

Compute n & (n - 1). Subtracting 1 clears the lowest set bit, so the result is 0 only when that bit was the only one. If it is not 0, n is not even a power of two.

3

Check that the bit is at an even position

AND n with 0x55555555. The mask keeps bits 0, 2, 4, …, 30 and drops the odd ones. A non-zero result means the single bit landed on an even position, so n = 2^(2x) = 4^x.

04

Code in Python, C++ and Java

▶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 & x55555555) != 0
bit76543210n1600010000padsvalue128643216842116 is positive → check its bits
n16binary 10000
n > 0yesguard passes
Every 4^x is positive, so the guard only stops zero and negatives. 16 is positive, so the code moves on to its bits: 10000.
bit76543210n1600010000padsvalue1286432168421one set bit, at position 4 → a power of 2
set bits1bit 4
n & (n - 1)0only bit cleared
n & (n - 1) clears the lowest set bit and leaves 0, so bit 4 is the only one: 16 = 2^4. Now the solid pads matter. They sit at the even positions, which is where every 4^x puts its bit, because 4^x = 2^(2x).
bit76543210n1600010000padsvalue1286432168421bit 4 is even → 16 = 4^2 → return true
n & 0x5555555516bit kept
resulttrue
The bit drops onto a solid pad. The mask 0x55555555 has a 1 at position 4, so the AND keeps the bit and is not 0. Since 4 = 2 × 2, 16 = 4^2.
05

Common pitfalls

Using only the mask

✗ Wrong
return n > 0 and (n & 0x55555555) != 0
✓ Right
return n > 0 and (n & (n - 1)) == 0 and (n & 0x55555555) != 0

n = 5 is 101: both bits are at even positions, so the mask alone says true. The one-bit test is what rules out numbers like 5, 17 and 21.

Mixing up the two masks

✗ Wrong
return n > 0 and (n & (n - 1)) == 0 and (n & 0xAAAAAAAA) != 0
✓ Right
return n > 0 and (n & (n - 1)) == 0 and (n & 0x55555555) != 0

0xAAAAAAAA has its 1s at the odd positions, so it keeps 2, 8 and 32 and drops 1, 4 and 16. With != 0 every answer flips: n = 16 returns false. That mask only works paired with == 0.

06

Edge cases

n = 1

1 = 4⁰. Its one bit is at position 0, which is even, so the mask keeps it and the answer is true with no special case.

n = 2³⁰ = 1,073,741,824

The largest power of four in a signed 32-bit int (4¹⁵). Bit 30 is even and the mask covers it, so it passes.

07

Complexity

Time
O(1)
Space
O(1)
Three comparisons and two ANDs for any n. Dividing by 4 in a loop would take up to 15 steps.
08

Power of Four FAQ

Is every power of 2 a power of 4?

No. Every power of 4 is a power of 2, but only the even powers of 2 are powers of 4: 1, 4, 16, 64 pass, while 2, 8, 32, 128 do not. That is exactly the gap the even-position mask closes.

Why does (n - 1) % 3 == 0 also work?

Once n is known to be a power of two, 4^x - 1 is always divisible by 3, because 4 ≡ 1 (mod 3). An odd power 2 · 4^x leaves remainder 2 mod 3, so 2 · 4^x - 1 leaves remainder 1. So n > 0, n & (n - 1) == 0 and (n - 1) % 3 == 0 together are another loop-free test.

What does 0x55555555 look like in binary?

Hex digit 5 is 0101, so eight of them give 0101 0101 … 0101: 32 bits with a 1 at positions 0, 2, 4, …, 30. Its partner 0xAAAAAAAA (hex A = 1010) has the 1s at the odd positions instead.