Power of Two
Determine if an integer is a power of two using recursive halving down to the base case of one.
01The problem
Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n == 2^x.
| Input | Output | Why |
|---|---|---|
n = 1 | true | 2^0 = 1 |
n = 16 | true | 2^4 = 16 |
n = 3 | false | Not divisible by 2 |
n = -16 | false | Negative numbers cannot be powers of two |
Constraints: -2^31 <= n <= 2^31 - 1.
02How to spot it
- A power of two is formed by repeatedly multiplying 2.
- In reverse: repeatedly dividing a power of two by 2 must eventually reach exactly
1. - If at any point before reaching
1the number is odd (n % 2 != 0), it contains an odd prime factor and can never be a power of two. - Zero and negative numbers are never powers of two.
So: recursive halving with odd guard.
03Pseudocode
isPow2(n):
if n <= 0: return false
if n == 1: return true
if n is odd: return false
return isPow2(n / 2)- If
n <= 0: returnfalse(zero and negatives cannot be powers of two). - If
n == 1: returntrue(reached2^0). - If
nis odd: returnfalse(cannot be halved evenly). - Recurse on
isPow2(n / 2).
04Watch it work
current n
16
status
start
Determine recursively if 16 is a power of two.
Pseudocode
- isPow2(n):
- if n <= 0: return false
- if n == 1: return true
- if n is odd: return false
- return isPow2(n / 2)
Variables
- n
- 16
05Step-by-step dry run
- 1
current n
16
status
start
Determine recursively if 16 is a power of two.
n = 16
- 2
current n
16
status
halving -> 8
16 is even. Halve to 8 and recurse: isPow2(8).
val = 16next = 8
- 3
current n
8
status
halving -> 4
8 is even. Halve to 4 and recurse: isPow2(4).
val = 8next = 4
- 4
current n
4
status
halving -> 2
4 is even. Halve to 2 and recurse: isPow2(2).
val = 4next = 2
- 5
current n
2
status
halving -> 1
2 is even. Halve to 1 and recurse: isPow2(1).
val = 2next = 1
- 6
current n
1
status
n == 1 -> true
Base case: 1 == 1 (2^0). Return true!
val = 1
- 7
result
true
Result: 16 is a power of two.
result = true
06Java solution
public class PowerOfTwo {
public static boolean isPowerOfTwo(int n) {
if (n <= 0) return false;
if (n == 1) return true;
if (n % 2 != 0) return false;
return isPowerOfTwo(n / 2);
}
public static void main(String[] args) {
System.out.println(isPowerOfTwo(1));
System.out.println(isPowerOfTwo(16));
System.out.println(isPowerOfTwo(3));
System.out.println(isPowerOfTwo(-16));
}
}true true false false
07Why it works
Every positive integer has a unique prime factorization.
A number is a power of two if and only if its only prime factor is 2.
If n is even, dividing by 2 strips one factor of 2 without affecting any other factors.
If n is odd and greater than 1, it must contain an odd factor (like 3 or 5), proving it cannot be a power of two.
When all factors of 2 are stripped, the quotient arrives at 1.
08Time and space
- Time complexity:
O(log n). The input is halved at each step, taking at most31recursive calls for 32-bit integers. - Space complexity:
O(log n)call stack depth, which is bounded by at most32frames.
09Edge cases
n <= 0: Zero and negative numbers returnfalseimmediately.n = 1: Base case2^0 = 1returnstrue.n = Integer.MIN_VALUE (-2^31): Handled byif (n <= 0) return false.
10Common mistakes
- Forgetting non-positive numbers.
-16is even and repeatedly divides to-1, which could loop infinitely or return the wrong answer if negative numbers aren't rejected upfront. - Missing the odd check. If you only check
if (n == 1) return true; else return isPowerOfTwo(n / 2);,isPowerOfTwo(6)divides6 / 2 = 3, then3 / 2 = 1(integer truncation!) and falsely returnstrue. You must checkn % 2 != 0.
11Other approaches
| Approach | Time | Space | Notes |
|---|---|---|---|
| Recursive halving | O(log n) | O(log n) stack | Clean recursive structure |
| Iterative loop | O(log n) | O(1) | While loop dividing by 2 |
| Bit manipulation | O(1) | O(1) | n > 0 && (n & (n - 1)) == 0 |
12In the interview
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. Why does integer division without the odd check falsely report 6 as a power of two?
2. What is the maximum recursion depth for any valid 32-bit integer?
14Solve it yourself
Try implementing isPowerOfThree (LeetCode #326) and isPowerOfFour (LeetCode #342):
- How does the modulus check change for base 3?
- For base 4, why is an extra check beyond power-of-two needed?