2Brute2Best
πŸ”₯0

Power of Two

EasyΒ·LeetCode #231 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

Given an integer n, decide whether it is a power of two. That means n = 2^x for some whole number x β‰₯ 0. Return true if it is, and false if it is not.

Follow-up: can you solve it without loops or recursion?

Input

An integer n. It can be zero or negative.

Output

true if n is a power of two, otherwise false.

Example 1
Input: n = 1
Output: true

1 = 2^0.

Example 2
Input: n = 16
Output: true

16 = 2^4.

Example 3
Input: n = 3
Output: false

No whole power of 2 is 3.

Constraints
  • β€’ βˆ’2^31 ≀ n ≀ 2^31 βˆ’ 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œis a power of two”
β†’ Exactly one 1 bit in binary, because 2^x has a single bit set.
β€œreturn true or false”
β†’ A yes/no answer, so one bit test is enough and no loop is needed.
β€œrange starts below zero”
β†’ Zero and negative numbers must be rejected before the bit trick.
03

Approach Complexity Ladder

Brute Force β€” halve until you reach 1O(log N)O(1)

Divide by 2 while even; the answer is yes only if you end at 1

Optimal β€” a single 1 bit testO(1)O(1)

A power of two has one 1 bit, so n & (n βˆ’ 1) is 0 exactly for powers of two (n > 0)

Brute Force Intuition

A power of two can be halved again and again, and it always ends at 1. Any other number gets stuck on an odd number that is not 1. So halve while the number is even, then check whether you reached 1.

Recognition rule for this sub-pattern:

n & (nβˆ’1) removes the lowest set bit; shifts read bits one at a time.
Remember this Β· Bit Manipulation
Trigger
"Every element appears twice except…", power of two, counting set bits, subsets as bitmasks, O(1) space tricks.
Think
What happens to this problem if I look at each bit separately?
Key question
Which bit operation (XOR, AND, OR, shift) cancels or isolates what I need?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.