Power of Two (LeetCode 231): Solution and Explanation

Easy
Math
Bit Manipulation
Recursion

Given an integer n, decide whether it equals 2 raised to some non-negative integer power. Values like 1, 2, 4 and 1024 qualify, while 0, negative numbers and values such as 6 do not.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

The straightforward approach is to keep dividing n by 2 while it is even. If you end up at exactly 1, the number was a power of two; if you hit an odd number greater than 1 first, it was not. That takes about log n steps, which is already fast, but the problem invites a solution with no loop at all.

Look at powers of two in binary: 1 is 1, 2 is 10, 4 is 100, 8 is 1000. Every one of them has exactly one bit set. So the question becomes whether n is positive and has a single 1 bit. Now think about what subtracting 1 does to a binary number. It flips the lowest set bit to 0 and turns every 0 below it into 1. For 8, that turns 1000 into 0111.

If n has exactly one set bit, then n and n - 1 share no 1 bits, so n & (n - 1) is 0. If n has two or more set bits, subtracting 1 only touches the lowest one, the higher bits survive in both numbers, and the AND is nonzero. That gives a constant-time test, with one important guard: zero also produces 0 & -1 = 0, and negative numbers are never powers of two, so you must check n > 0 first.

Approach: Clear the lowest set bit

  1. Check that n > 0, because zero and negative values can never be a power of two.
  2. Compute n - 1, which flips the lowest set bit of n and every bit below it.
  3. Take n & (n - 1), which equals n with its lowest set bit removed.
  4. If that result is 0, n had exactly one set bit, so return True; otherwise return False.

Step-by-step example

n = 8, then n = 6
  1. For n = 8, the binary form is 1000 and n - 1 = 7 is 0111.
  2. The AND of 1000 and 0111 is 0000, and since 8 is positive, the function returns True.
  3. For n = 6, the binary form is 110 and n - 1 = 5 is 101.
  4. The AND of 110 and 101 is 100, which is 4, not 0, so the function returns False because 6 has two set bits.

Code: Python, Java, C++

1class Solution:
2    def isPowerOfTwo(self, n: int) -> bool:
3        return n > 0 and (n & (n - 1)) == 0

Time and space complexity

Time

O(1)

The check is one comparison, one subtraction and one bitwise AND, regardless of the value of n. There is no loop, so even the largest 32-bit input takes the same few machine instructions as n = 1.

Space

O(1)

No extra storage is used beyond the temporary result of the expression.

Other approaches

Repeated division (time O(log n), space O(1))

While n is even and positive, divide it by 2, then check whether it reached 1. It is easy to explain and works in any language, at the cost of a loop of up to about 31 iterations for 32-bit inputs.

Divisor of the largest power (time O(1), space O(1))

For 32-bit signed inputs, the largest power of two is 2^30 = 1073741824. A positive n is a power of two exactly when it divides that number evenly, because the only divisors of a power of two are smaller powers of two.

Common mistakes

  • Dropping the n > 0 check. Without it, 0 passes because 0 & -1 is 0, and in some languages the most negative integer also passes.
  • Writing n & n - 1 == 0 without parentheses in Java or C++. Comparison binds tighter than &, so the expression is evaluated as n & ((n - 1) == 0).
  • Using floating point logarithms such as log2(n) and checking for an integer result. Rounding errors can misclassify large values.
  • Running the division loop on zero. 0 % 2 is 0, so a loop that divides while the number is even never terminates.

Frequently asked questions

Why does n & (n - 1) == 0 check for a power of two?

Subtracting 1 clears the lowest set bit and sets the bits below it. If that was the only set bit, n and n - 1 have no 1 bits in common and the AND is zero; any additional set bit would survive the AND.

Is 1 a power of two?

Yes, 1 is 2 to the power 0. The bit trick handles it correctly, since 1 & 0 is 0 and 1 is positive.

Can you solve Power of Two without loops or recursion?

Yes. Both the n & (n - 1) test and the divisibility check against 2^30 run in constant time with no loop.

How is this related to counting set bits?

Repeatedly applying n &= n - 1 until n is 0 counts the number of set bits. A power of two is simply the case where that count is exactly 1.