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 = 1true (because 2^0 = 1)
  • n = 16true (because 2^4 = 16)
  • n = 3false (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
Power of Two: Is 16 equal to 2^x?Inputn = 16Binary10000Check2^4 = 16 ✓ResulttruePowers of 2 in binary: 1, 10, 100, 1000, 10000, 100000...Each has exactly ONE bit set to 1Key: Single bit = Power of 2
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
Asked in
Google 25 Amazon 32 Microsoft 18 Apple 15
89.0K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen