Skip to content
Veltoryx Technologies
FreeProblem 67● EasyLeetCode #231 ↗4 min readUpdated 30 October 2026

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.

InputOutputWhy
n = 1true2^0 = 1
n = 16true2^4 = 16
n = 3falseNot divisible by 2
n = -16falseNegative 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 1 the 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

text
isPow2(n):
  if n <= 0: return false
  if n == 1: return true
  if n is odd: return false
  return isPow2(n / 2)
  1. If n <= 0: return false (zero and negatives cannot be powers of two).
  2. If n == 1: return true (reached 2^0).
  3. If n is odd: return false (cannot be halved evenly).
  4. Recurse on isPow2(n / 2).

04Watch it work

Visualizer · step through the algorithmn = 16

current n

16

status

start

Determine recursively if 16 is a power of two.

Pseudocode

  1. isPow2(n):
  2. if n <= 0: return false
  3. if n == 1: return true
  4. if n is odd: return false
  5. return isPow2(n / 2)

Variables

n
16
step 1 / 7

05Step-by-step dry run

  1. 1

    current n

    16

    status

    start

    Determine recursively if 16 is a power of two.

    n = 16

  2. 2

    current n

    16

    status

    halving -> 8

    16 is even. Halve to 8 and recurse: isPow2(8).

    val = 16next = 8

  3. 3

    current n

    8

    status

    halving -> 4

    8 is even. Halve to 4 and recurse: isPow2(4).

    val = 8next = 4

  4. 4

    current n

    4

    status

    halving -> 2

    4 is even. Halve to 2 and recurse: isPow2(2).

    val = 4next = 2

  5. 5

    current n

    2

    status

    halving -> 1

    2 is even. Halve to 1 and recurse: isPow2(1).

    val = 2next = 1

  6. 6

    current n

    1

    status

    n == 1 -> true

    Base case: 1 == 1 (2^0). Return true!

    val = 1

  7. 7

    result

    true

    Result: 16 is a power of two.

    result = true

06Java solution

PowerOfTwo.java
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));
    }
}
Expected output
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 most 31 recursive calls for 32-bit integers.
  • Space complexity: O(log n) call stack depth, which is bounded by at most 32 frames.

09Edge cases

  • n <= 0: Zero and negative numbers return false immediately.
  • n = 1: Base case 2^0 = 1 returns true.
  • n = Integer.MIN_VALUE (-2^31): Handled by if (n <= 0) return false.

10Common mistakes

  • Forgetting non-positive numbers. -16 is 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) divides 6 / 2 = 3, then 3 / 2 = 1 (integer truncation!) and falsely returns true. You must check n % 2 != 0.

11Other approaches

ApproachTimeSpaceNotes
Recursive halvingO(log n)O(log n) stackClean recursive structure
Iterative loopO(log n)O(1)While loop dividing by 2
Bit manipulationO(1)O(1)n > 0 && (n & (n - 1)) == 0

12In the interview

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. Why does integer division without the odd check falsely report 6 as a power of two?

  2. 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):

  1. How does the modulus check change for base 3?
  2. For base 4, why is an extra check beyond power-of-two needed?