Back to DSA
Power of Two
mediumDetermine whether a given integer is an exact power of two. An integer n qualifies if there exists some non-negative integer k such that 2^k equals n.
Examples
Example 1:
Input:
n = 8Output:
true Example 2:
Input:
n = 10Output:
falseHints
class Solution {
public boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
}Time complexity
O(1)Space complexity
O(1)