Back to DSA

Power of Two

medium
Acceptance: 43%
Bit ManipulationMath

Determine 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 = 8
Output:true
Example 2:
Input:n = 10
Output:false

Hints

00:00
class Solution {
    public boolean isPowerOfTwo(int n) {
        return n > 0 && (n & (n - 1)) == 0;
    }
}
Time complexityO(1)
Space complexityO(1)