Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Easy Bit Manipulation Medium frequency Solution Available Practice Mode

Power of Two LeetCode Solution

Tags: #Math #Bit Manipulation #Recursion

Approach Summary

A power of two in binary has exactly one 1-bit. Check: n > 0 && (n & (n-1)) == 0.

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Bit Manipulation Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Bit Manipulation pattern.

Framework: Bit Manipulation • Interview Prep: Tested at FAANG • Explore all Bit Manipulation problems & invariants →

How to Recognize This Pattern

  • Powers of two have a single set bit
  • n & (n-1) clears the lowest set bit — result should be 0

Complexity Analysis

Time Complexity

O(1)

Space Complexity

O(1)

Tags

Math Bit Manipulation Recursion

Frequently Asked Questions

What is the optimal time and space complexity for Power of Two?
The optimal solution for Power of Two achieves O(1) runtime complexity and requires O(1) auxiliary space. Time Complexity: O(1) — each element or state is traversed and evaluated in bounded amortized time without redundant reprocessing. Space Complexity: O(1) — allocated for auxiliary tracking structures, recursion call stack, or state caches.
How do I identify that Power of Two requires the Bit Manipulation pattern?
In technical interviews, look for key problem signals: Powers of two have a single set bit; n & (n-1) clears the lowest set bit — result should be 0. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Bit Manipulation strategy.
What critical edge cases must be handled in Power of Two?
Essential edge cases to verify include: Key recognition trigger: Powers of two have a single set bit.; Constraint condition: n & (n-1) clears the lowest set bit — result should be 0.; Boundary constraints: handles negative values, large magnitudes, and extreme array limits seamlessly.; Suboptimal inputs: runs strictly within the O(1) time budget without stack overflow or time limit exceeded.. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Power of Two?
Power of Two is a Easy-difficulty question tagged with medium recurrence. It is actively asked in technical screening and onsite interview loops at Google, Apple.
Buy me a coffee
Daily Practice
0/0