Power of Two
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?
An integer n. It can be zero or negative.
true if n is a power of two, otherwise false.
1 = 2^0.
16 = 2^4.
No whole power of 2 is 3.
- β’ β2^31 β€ n β€ 2^31 β 1
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Divide by 2 while even; the answer is yes only if you end at 1
A power of two has one 1 bit, so n & (n β 1) is 0 exactly for powers of two (n > 0)
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.