|
| 1 | +--- |
| 2 | +id: powerof-four |
| 3 | +title: Power of Four |
| 4 | +sidebar_label: 342-Power of Four |
| 5 | +tags: |
| 6 | + - Math |
| 7 | + - Bit Manipulation |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | +description: "This is a solution to the Power of Four problem on LeetCode." |
| 13 | +sidebar_position: 6 |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +Given an integer `n`, return `true` if it is a power of four. Otherwise, return `false`. |
| 19 | + |
| 20 | +An integer `n` is a power of four if there exists an integer `x` such that `n == 4^x`. |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +**Example 1:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: n = 16 |
| 28 | +Output: true |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +``` |
| 34 | +Input: n = 5 |
| 35 | +Output: false |
| 36 | +``` |
| 37 | + |
| 38 | +**Example 3:** |
| 39 | + |
| 40 | +``` |
| 41 | +Input: n = 1 |
| 42 | +Output: true |
| 43 | +``` |
| 44 | + |
| 45 | +### Constraints |
| 46 | + |
| 47 | +- `-2^31 <= n <= 2^31 - 1` |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## Solution for Power of Four Problem |
| 54 | + |
| 55 | +### Approach 1: Brute Force (Loop/Recursion) |
| 56 | + |
| 57 | +The brute force approach involves repeatedly dividing the number by `4` and checking if it becomes `1`. |
| 58 | + |
| 59 | +#### Code in Different Languages |
| 60 | + |
| 61 | +<Tabs> |
| 62 | +<TabItem value="C++" label="C++" default> |
| 63 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 64 | + |
| 65 | +```cpp |
| 66 | +class Solution { |
| 67 | +public: |
| 68 | + bool isPowerOfFour(int n) { |
| 69 | + if (n < 1) return false; |
| 70 | + while (n % 4 == 0) { |
| 71 | + n /= 4; |
| 72 | + } |
| 73 | + return n == 1; |
| 74 | + } |
| 75 | +}; |
| 76 | +``` |
| 77 | +
|
| 78 | +</TabItem> |
| 79 | +<TabItem value="Java" label="Java"> |
| 80 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 81 | +
|
| 82 | +```java |
| 83 | +class Solution { |
| 84 | + public boolean isPowerOfFour(int n) { |
| 85 | + if (n < 1) return false; |
| 86 | + while (n % 4 == 0) { |
| 87 | + n /= 4; |
| 88 | + } |
| 89 | + return n == 1; |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +</TabItem> |
| 95 | +<TabItem value="Python" label="Python"> |
| 96 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 97 | + |
| 98 | +```python |
| 99 | +class Solution: |
| 100 | + def isPowerOfFour(self, n: int) -> bool: |
| 101 | + if n < 1: |
| 102 | + return False |
| 103 | + while n % 4 == 0: |
| 104 | + n //= 4 |
| 105 | + return n == 1 |
| 106 | +``` |
| 107 | + |
| 108 | +</TabItem> |
| 109 | +</Tabs> |
| 110 | + |
| 111 | +#### Complexity Analysis |
| 112 | + |
| 113 | +- **Time Complexity**: $O(\log n)$, as we keep dividing `n` by `4`. |
| 114 | +- **Space Complexity**: $O(1)$, constant space usage. |
| 115 | + |
| 116 | +### Approach 2: Optimized (Bit Manipulation) |
| 117 | + |
| 118 | +The optimized approach checks if `n` is a power of four without loops or recursion. We can use bit manipulation: |
| 119 | + |
| 120 | +1. `n > 0`: Ensure `n` is positive. |
| 121 | +2. `n & (n - 1) == 0`: Ensure `n` is a power of two (only one bit set). |
| 122 | +3. `(n - 1) % 3 == 0`: Ensure `n` is a power of four (using properties of powers of four). |
| 123 | + |
| 124 | +#### Code in Different Languages |
| 125 | + |
| 126 | +<Tabs> |
| 127 | +<TabItem value="C++" label="C++" default> |
| 128 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 129 | + |
| 130 | +```cpp |
| 131 | +class Solution { |
| 132 | +public: |
| 133 | + bool isPowerOfFour(int n) { |
| 134 | + return n > 0 && (n & (n - 1)) == 0 && (n - 1) % 3 == 0; |
| 135 | + } |
| 136 | +}; |
| 137 | +``` |
| 138 | +
|
| 139 | +</TabItem> |
| 140 | +<TabItem value="Java" label="Java"> |
| 141 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 142 | +
|
| 143 | +```java |
| 144 | +class Solution { |
| 145 | + public boolean isPowerOfFour(int n) { |
| 146 | + return n > 0 && (n & (n - 1)) == 0 && (n - 1) % 3 == 0; |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +</TabItem> |
| 152 | +<TabItem value="Python" label="Python"> |
| 153 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 154 | + |
| 155 | +```python |
| 156 | +class Solution: |
| 157 | + def isPowerOfFour(self, n: int) -> bool: |
| 158 | + return n > 0 and (n & (n - 1)) == 0 and (n - 1) % 3 == 0 |
| 159 | +``` |
| 160 | + |
| 161 | +</TabItem> |
| 162 | +</Tabs> |
| 163 | + |
| 164 | +#### Complexity Analysis |
| 165 | + |
| 166 | +- **Time Complexity**: $O(1)$, constant time operations. |
| 167 | +- **Space Complexity**: $O(1)$, constant space usage. |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +<h2>Authors:</h2> |
| 172 | + |
| 173 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 174 | +{['ImmidiSivani'].map(username => ( |
| 175 | + <Author key={username} username={username} /> |
| 176 | +))} |
| 177 | +</div> |
0 commit comments