231. Power of Two
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Bitwise Manipulation
This problem can be solved efficiently using bitwise operations, which perfectly addresses the follow-up question of avoiding loops and recursion.
Solution
Power of Two in Python
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
# A power of two must be a positive number.
# The check n > 0 is crucial to handle the n=0 case (as 0 & -1 = 0) and negative numbers.
if n <= 0:
return False
# The expression (n & (n - 1)) == 0 checks if n has only one bit set to 1.
# This is a characteristic of numbers that are a power of two.
# For example, n = 8 (1000), n-1 = 7 (0111), n & (n-1) = 0.
# For example, n = 6 (0110), n-1 = 5 (0101), n & (n-1) = 4 != 0.
return (n & (n - 1)) == 0
Power of Two in Java
class Solution {
public boolean isPowerOfTwo(int n) {
// First, check if n is positive. Powers of two are always positive (2^0=1, 2^1=2, etc.).
// The n > 0 check handles n=0 and all negative numbers.
if (n <= 0) {
return false;
}
// A key property of a power of two is that in its binary representation, it has exactly one '1' bit.
// For such a number n, n-1 will have all bits flipped after the '1' bit position.
// For example, if n = 8 (binary 1000), n-1 = 7 (binary 0111).
// Performing a bitwise AND (n & (n-1)) will result in 0.
// This is a very efficient way to check if a number has only one set bit.
return (n & (n - 1)) == 0;
}
}
Power of Two in C++
class Solution {
public:
bool isPowerOfTwo(int n) {
// First, powers of two must be positive numbers. So, if n is not positive, return false.
// e.g., 2^0=1, 2^1=2, 2^2=4.
if (n <= 0) {
return false;
}
// A key property of a power of two is that in its binary representation, it has exactly one '1' bit.
// For example, 8 is '1000' in binary.
// n - 1 for a power of two will have all bits flipped after the '1' bit's position.
// For example, if n = 8 (binary 1000), n-1 = 7 (binary 0111).
// Performing a bitwise AND on n and (n-1) will result in 0 if n is a power of two.
// e.g., 8 & 7 => 1000 & 0111 = 0000
// If n is not a power of two, it has more than one '1' bit, so the result will not be 0.
// e.g., 6 & 5 => 0110 & 0101 = 0100
return (n & (n - 1)) == 0;
}
};
Power of Two in TypeScript
function isPowerOfTwo(n: number): boolean {
// Powers of two must be positive. e.g., 2^0 = 1, 2^1 = 2, etc.
// The n > 0 check correctly filters out 0 and all negative integers.
if (n <= 0) {
return false;
}
// A number that is a power of two has a binary representation with exactly one '1' bit.
// e.g., 8 is 1000 in binary.
// Subtracting 1 from such a number flips the '1' bit to '0' and all subsequent '0's to '1's.
// e.g., 7 is 0111 in binary.
// A bitwise AND between n and n-1 will therefore always be 0 for a power of two.
// n & (n-1)
// For n=8: 1000 & 0111 = 0000
return (n & (n - 1)) === 0;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.