|
| 1 | +# Power of Two |
| 2 | + |
| 3 | +**Difficulty:** Easy |
| 4 | +**Category:** Bit Manipulation, Math, Recursion |
| 5 | +**Leetcode Link:** [Problem Link](https://leetcode.com/problems/power-of-two/) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 📝 Introduction |
| 10 | + |
| 11 | +Given an integer `n`, return `true` if it is a power of two, otherwise return `false`. |
| 12 | + |
| 13 | +An integer is a power of two if there exists an integer `x` such that `n == 2^x`. |
| 14 | + |
| 15 | +**Constraints:** |
| 16 | +- -2^31 <= n <= 2^31 - 1 |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 💡 Approach & Key Insights |
| 21 | + |
| 22 | +### Key Observation |
| 23 | +A power of two in binary form has exactly **one bit set to 1**. |
| 24 | +Examples: |
| 25 | +- 1 → `0001` |
| 26 | +- 2 → `0010` |
| 27 | +- 4 → `0100` |
| 28 | +- 8 → `1000` |
| 29 | + |
| 30 | +For a number `n` that is a power of two: |
| 31 | +- `n > 0` (since negative numbers and zero cannot be powers of two) |
| 32 | +- `(n & (n - 1)) == 0` (removes the only set bit, leaving 0) |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## 🛠️ Breakdown of Approaches |
| 37 | + |
| 38 | +### 1️⃣ Brute Force / Naive Approach |
| 39 | + |
| 40 | +- **Explanation:** |
| 41 | + Keep dividing `n` by 2 while it is even. If the result becomes 1, then it’s a power of two. |
| 42 | +- **Time Complexity:** O(log n) — because we keep halving the number. |
| 43 | +- **Space Complexity:** O(1) |
| 44 | + |
| 45 | +Example: |
| 46 | +`n = 16` → 16 → 8 → 4 → 2 → 1 → return `true` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +### 2️⃣ Best / Optimized Approach (Bit Manipulation) |
| 51 | + |
| 52 | +- **Explanation:** |
| 53 | + For powers of two, `n & (n - 1)` will be 0, and `n` must be positive. |
| 54 | +- **Time Complexity:** O(1) — single check. |
| 55 | +- **Space Complexity:** O(1) |
| 56 | + |
| 57 | +Example: |
| 58 | +``` |
| 59 | +n = 8 (1000) |
| 60 | +n-1 = 7 (0111) |
| 61 | +n & (n-1) = 0 → Power of two |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## 📊 Complexity Analysis |
| 67 | + |
| 68 | +| Approach | Time Complexity | Space Complexity | |
| 69 | +| -------------- | --------------- | ---------------- | |
| 70 | +| Brute Force | O(log n) | O(1) | |
| 71 | +| Bitwise Check | O(1) | O(1) | |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## 📌 Example Walkthroughs & Dry Runs |
| 76 | + |
| 77 | +Example: |
| 78 | +``` |
| 79 | +Input: n = 16 |
| 80 | +Binary: 10000 |
| 81 | +n - 1 = 01111 |
| 82 | +n & (n - 1) = 0 |
| 83 | +Output: true |
| 84 | +``` |
| 85 | + |
| 86 | +Example: |
| 87 | +``` |
| 88 | +Input: n = 3 |
| 89 | +Binary: 11 |
| 90 | +n - 1 = 10 |
| 91 | +n & (n - 1) = 10 (non-zero) |
| 92 | +Output: false |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## 🔗 Additional Resources |
| 98 | + |
| 99 | +- [Bit Manipulation Basics](https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/) |
| 100 | +- [LeetCode Discuss - Power of Two](https://leetcode.com/problems/power-of-two/discuss/) |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +Author: Kailash Senthilkumar |
| 105 | +Date: 09/08/2025 |
0 commit comments