231. Power of Two - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Given an integer n, return true if n 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ˣ.

Examples

  1. Input: n = 1
    Output: true
    Explanation: 1 = 2⁰.
  2. Input: n = 16
    Output: true
    Explanation: 16 = 2⁴.
  3. Input: n = 218
    Output: false
    Explanation: 218 is not a power of two.

Constraints

  • –2³¹ ≤ n ≤ 2³¹ – 1

Hints

  1. How many bits are set in the binary representation of a power of two?
  2. If you subtract 1 from a power of two, what happens to its binary form?

Approach 1 Brute Force Division

Explanation

Keep dividing n by 2 as long as it’s even and greater than 0. If you end at 1, it was a power of two; otherwise not.

Step‑by‑step Walkthrough

  1. If n ≤ 0, return false.
  2. While n % 2 == 0, do n /= 2.
  3. After the loop, check if n == 1.

Visual Example

n = 16  
16 % 2 == 0 → n = 8  
8 % 2 == 0  → n = 4  
4 % 2 == 0  → n = 2  
2 % 2 == 0  → n = 1  
Loop ends; n == 1 → true

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time O(log n) because each division halves n.
  • Space O(1).

Approach 2 Bit Manipulation (Optimal)

Explanation

A positive power of two has exactly one bit set in its binary form (100…0). For such a number n, the expression n & (n - 1) clears that lowest set bit, resulting in zero. So n > 0 && (n & (n - 1)) == 0 detects powers of two in constant time.

Step‑by‑step Walkthrough

  1. If n ≤ 0, return false.
  2. Compute n & (n - 1).
  3. If the result is 0, n had only one '1' bit → return true; else false.

Visual Example

n = 16 → binary 10000  
n - 1 = 15 → binary 01111  
10000 & 01111 = 00000 → zero → true  

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time O(1).
  • Space O(1).

Common Mistakes

  • Not handling non‑positive n (should return false for zero and negatives).
  • Using floating‑point checks like repeated division by 2.0 which can incur precision errors.
  • Forgetting that (n & (n - 1)) trick only applies to integers.

Edge Cases

  • n = 0false
  • Negative nfalse
  • Maximum 32‑bit integer n = 2³¹ - 1 → not a power of two → false.

Alternative Variations

  • Check if a number is a power of three or power of four using similar bit or math tricks.
  • Count the number of set bits and ensure it equals 1.
  • Use logarithms: log₂(n) is integer (watch floating‑point).
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;