Power of Two - Problem
Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two if there exists an integer x such that n == 2^x.
Examples:
n = 1→true(because 2^0 = 1)n = 16→true(because 2^4 = 16)n = 3→false(no integer x exists)
Input & Output
Example 1 — Power of Two
$
Input:
n = 1
›
Output:
true
💡 Note:
1 is 2⁰, so it's a power of two
Example 2 — Perfect Power
$
Input:
n = 16
›
Output:
true
💡 Note:
16 is 2⁴, so it's a power of two
Example 3 — Not a Power
$
Input:
n = 3
›
Output:
false
💡 Note:
3 cannot be expressed as 2^x for any integer x
Constraints
- -2³¹ ≤ n ≤ 2³¹ - 1
Visualization
Tap to expand
Understanding the Visualization
1
Input
Integer n = 16
2
Check
Verify if 16 = 2^x for some x
3
Output
Return true (16 = 2^4)
Key Takeaway
🎯 Key Insight: Powers of 2 have exactly one bit set in binary
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code