Please upgrade to NeetCode Pro to view company tags.
Prerequisites
Before attempting this problem, you should be comfortable with:
Bit Manipulation - Understanding binary representation of numbers and bitwise operators (AND, OR, shifts) is essential for the optimal solutions
Recursion - The recursive approach divides the problem by 2 at each step until reaching the base case
Two's Complement - Understanding how negative numbers are represented helps explain why n & (-n) isolates the lowest set bit
1. Brute Force
Intuition
We can generate all powers of two by starting from 1 and repeatedly multiplying by 2. If we reach exactly n, then n is a power of two. If we exceed n without matching it, then it's not.
classSolution:defisPowerOfTwo(self, n:int)->bool:if n <=0:returnFalse
x =1while x < n:
x *=2return x == n
publicclassSolution{publicbooleanisPowerOfTwo(int n){if(n <=0)returnfalse;long x =1;while(x < n){
x *=2;}return x == n;}}
classSolution{public:boolisPowerOfTwo(int n){if(n <=0)returnfalse;longlong x =1;while(x < n){
x *=2;}return x == n;}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isPowerOfTwo(n){if(n <=0)returnfalse;let x =1;while(x < n){
x *=2;}return x === n;}}
publicclassSolution{publicboolIsPowerOfTwo(int n){if(n <=0){returnfalse;}int x =1;while(x < n){
x *=2;}return x == n;}}
funcisPowerOfTwo(n int)bool{if n <=0{returnfalse}
x :=1for x < n {
x *=2}return x == n
}
class Solution {funisPowerOfTwo(n: Int): Boolean {if(n <=0)returnfalsevar x =1Lwhile(x < n){
x *=2}return x == n.toLong()}}
classSolution{funcisPowerOfTwo(_ n:Int)->Bool{if n <=0{returnfalse}var x =1while x < n {
x *=2}return x == n
}}
Time & Space Complexity
Time complexity: O(logn)
Space complexity: O(1)
2. Recursion
Intuition
A number is a power of two if we can repeatedly divide it by 2 until we reach 1. If at any point the number is odd (and not 1), it cannot be a power of two.
This recursive approach reduces the problem by half at each step.
Algorithm
If n equals 1, return true (2^0 = 1).
If n is less than or equal to 0, or n is odd, return false.
classSolution:defisPowerOfTwo(self, n:int)->bool:if n ==1:returnTrueif n <=0or n %2==1:returnFalsereturn self.isPowerOfTwo(n //2)
publicclassSolution{publicbooleanisPowerOfTwo(int n){if(n ==1){returntrue;}if(n <=0|| n %2==1){returnfalse;}returnisPowerOfTwo(n /2);}}
classSolution{public:boolisPowerOfTwo(int n){if(n ==1){returntrue;}if(n <=0|| n %2==1){returnfalse;}returnisPowerOfTwo(n /2);}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isPowerOfTwo(n){if(n ===1){returntrue;}if(n <=0|| n %2===1){returnfalse;}returnthis.isPowerOfTwo(Math.floor(n /2));}}
publicclassSolution{publicboolIsPowerOfTwo(int n){if(n ==1){returntrue;}if(n <=0|| n %2==1){returnfalse;}returnIsPowerOfTwo(n /2);}}
funcisPowerOfTwo(n int)bool{if n ==1{returntrue}if n <=0|| n%2==1{returnfalse}returnisPowerOfTwo(n /2)}
class Solution {funisPowerOfTwo(n: Int): Boolean {if(n ==1){returntrue}if(n <=0|| n %2==1){returnfalse}returnisPowerOfTwo(n /2)}}
classSolution{funcisPowerOfTwo(_ n:Int)->Bool{if n ==1{returntrue}if n <=0|| n %2==1{returnfalse}returnisPowerOfTwo(n /2)}}
Time & Space Complexity
Time complexity: O(logn)
Space complexity: O(logn) for recursion stack.
3. Iteration
Intuition
The same logic as recursion, but implemented with a loop. We keep dividing by 2 (or right-shifting by 1) as long as the number is even. If we end up with 1, the original number was a power of two.
Algorithm
If n is less than or equal to 0, return false.
While n is even (divisible by 2), right-shift n by 1.
classSolution:defisPowerOfTwo(self, n:int)->bool:if n <=0:returnFalsewhile n %2==0:
n >>=1return n ==1
publicclassSolution{publicbooleanisPowerOfTwo(int n){if(n <=0)returnfalse;while(n %2==0){
n >>=1;}return n ==1;}}
classSolution{public:boolisPowerOfTwo(int n){if(n <=0)returnfalse;while(n %2==0){
n >>=1;}return n ==1;}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isPowerOfTwo(n){if(n <=0)return0;while(n %2===0){
n >>=1;}return n ===1;}}
publicclassSolution{publicboolIsPowerOfTwo(int n){if(n <=0){returnfalse;}while(n %2==0){
n >>=1;}return n ==1;}}
funcisPowerOfTwo(n int)bool{if n <=0{returnfalse}for n%2==0{
n >>=1}return n ==1}
class Solution {funisPowerOfTwo(n: Int): Boolean {if(n <=0)returnfalsevar num = n
while(num %2==0){
num = num shr1}return num ==1}}
classSolution{funcisPowerOfTwo(_ n:Int)->Bool{if n <=0{returnfalse}var num = n
while num %2==0{
num >>=1}return num ==1}}
Time & Space Complexity
Time complexity: O(logn)
Space complexity: O(1)
4. Bit Manipulation - I
Intuition
In two's complement representation, -n flips all bits of n and adds 1. For a power of two (which has exactly one set bit), n & (-n) isolates the lowest set bit. If n is a power of two, this equals n itself since there's only one bit set.
Algorithm
Check that n is positive.
Compute n & (-n) to isolate the lowest set bit.
Return true if this equals n, meaning n has exactly one set bit.
classSolution:defisPowerOfTwo(self, n:int)->bool:return n >0and(n &(-n))== n
publicclassSolution{publicbooleanisPowerOfTwo(int n){return n >0&&(n &(-n))== n;}}
classSolution{public:boolisPowerOfTwo(int n){return n >0&&(n &(-n))== n;}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isPowerOfTwo(n){return n >0&&(n &-n)=== n;}}
publicclassSolution{publicboolIsPowerOfTwo(int n){return n >0&&(n &-n)== n;}}
funcisPowerOfTwo(n int)bool{return n >0&&(n&(-n))== n
}
class Solution {funisPowerOfTwo(n: Int): Boolean {return n >0&&(n and(-n))== n
}}
classSolution{funcisPowerOfTwo(_ n:Int)->Bool{return n >0&&(n &(-n))== n
}}
Time & Space Complexity
Time complexity: O(1)
Space complexity: O(1)
5. Bit Manipulation - II
Intuition
Powers of two in binary have exactly one bit set (1, 10, 100, 1000, ...). Subtracting 1 from such a number flips all bits from the rightmost set bit onward. For example, 8 (1000) minus 1 equals 7 (0111).
ANDing n with n - 1 clears the lowest set bit. If n is a power of two, this results in 0 since there was only one bit to clear.
Algorithm
Check that n is positive.
Compute n & (n - 1) to clear the lowest set bit.
Return true if the result is 0, meaning n had exactly one set bit.
classSolution:defisPowerOfTwo(self, n:int)->bool:return n >0and(n &(n -1))==0
publicclassSolution{publicbooleanisPowerOfTwo(int n){return n >0&&(n &(n -1))==0;}}
classSolution{public:boolisPowerOfTwo(int n){return n >0&&(n &(n -1))==0;}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isPowerOfTwo(n){return n >0&&(n &(n -1))===0;}}
publicclassSolution{publicboolIsPowerOfTwo(int n){return n >0&&(n &(n -1))==0;}}
funcisPowerOfTwo(n int)bool{return n >0&&(n&(n-1))==0}
class Solution {funisPowerOfTwo(n: Int): Boolean {return n >0&&(n and(n -1))==0}}
classSolution{funcisPowerOfTwo(_ n:Int)->Bool{return n >0&&(n &(n -1))==0}}
Time & Space Complexity
Time complexity: O(1)
Space complexity: O(1)
6. Math
Intuition
The largest power of two that fits in a 32-bit signed integer is 2^30 (since 2^31 exceeds the positive range). Any smaller power of two must divide evenly into 2^30.
If n is a power of two, then 2^30 mod n equals 0. If n is not a power of two, the division will have a remainder.
classSolution:defisPowerOfTwo(self, n:int)->bool:return n >0and((1<<30)% n)==0
publicclassSolution{publicbooleanisPowerOfTwo(int n){return n >0&&((1<<30)% n)==0;}}
classSolution{public:boolisPowerOfTwo(int n){return n >0&&((1<<30)% n)==0;}};
classSolution{/**
* @param {number} n
* @return {boolean}
*/isPowerOfTwo(n){return n >0&&(1<<30)% n ===0;}}
publicclassSolution{publicboolIsPowerOfTwo(int n){return n >0&&((1<<30)% n)==0;}}
funcisPowerOfTwo(n int)bool{return n >0&&(1<<30)%n ==0}
class Solution {funisPowerOfTwo(n: Int): Boolean {return n >0&&((1shl30)% n)==0}}
classSolution{funcisPowerOfTwo(_ n:Int)->Bool{return n >0&&((1<<30)% n)==0}}
Time & Space Complexity
Time complexity: O(1)
Space complexity: O(1)
Common Pitfalls
Forgetting to Handle Non-Positive Numbers
A common mistake is forgetting that n must be strictly positive. Zero and negative numbers cannot be powers of two, but the bit manipulation tricks like n & (n - 1) == 0 will incorrectly return true for n = 0. Always check n > 0 before applying bit operations.
Integer Overflow in Iterative Approaches
When iteratively multiplying by 2 (e.g., x *= 2), the variable can overflow if using a 32-bit integer type. In languages like Java or C++, use a long or long long type for the accumulator to avoid wrapping around to negative values before reaching large inputs like 2^30.