|
| 1 | +--- |
| 2 | +id: binary-number-with-alternating-bits |
| 3 | +title: Binary Number with Alternating Bits |
| 4 | +sidebar_label: 0693 - Binary Number with Alternating Bits |
| 5 | +tags: |
| 6 | + - Bit Manipulation |
| 7 | + - String |
| 8 | + - Bitmask |
| 9 | +description: "This is a solution to the Binary Number with Alternating Bits problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. |
| 15 | + |
| 16 | +### Examples |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +``` |
| 21 | +Input: n = 5 |
| 22 | +Output: true |
| 23 | +Explanation: The binary representation of 5 is: 101 |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +``` |
| 29 | +Input: n = 7 |
| 30 | +Output: false |
| 31 | +Explanation: The binary representation of 7 is: 111. |
| 32 | +``` |
| 33 | + |
| 34 | +### Constraints |
| 35 | + |
| 36 | +- $$1 \leq n \leq 2^{31} - 1$$ |
| 37 | + |
| 38 | +## Solution for Binary Number with Alternating Bits |
| 39 | + |
| 40 | +### Approach 1: Convert to String |
| 41 | +#### Intuition and Algorithm |
| 42 | + |
| 43 | +Let's convert the given number into a string of binary digits. Then, we should simply check that no two adjacent digits are the same. |
| 44 | + |
| 45 | +## Code in Different Languages |
| 46 | + |
| 47 | +<Tabs> |
| 48 | +<TabItem value="java" label="Java"> |
| 49 | + <SolutionAuthor name="@Shreyash3087"/> |
| 50 | + |
| 51 | +```java |
| 52 | +class Solution { |
| 53 | + public boolean hasAlternatingBits(int n) { |
| 54 | + String bits = Integer.toBinaryString(n); |
| 55 | + for (int i = 0; i < bits.length() - 1; i++) { |
| 56 | + if (bits.charAt(i) == bits.charAt(i+1)) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + return true; |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +</TabItem> |
| 66 | +<TabItem value="python" label="Python"> |
| 67 | + <SolutionAuthor name="@Shreyash3087"/> |
| 68 | + |
| 69 | +```python |
| 70 | +class Solution(object): |
| 71 | + def hasAlternatingBits(self, n): |
| 72 | + bits = bin(n) |
| 73 | + return all(bits[i] != bits[i+1] |
| 74 | + for i in xrange(len(bits) - 1)) |
| 75 | +``` |
| 76 | +</TabItem> |
| 77 | +</Tabs> |
| 78 | + |
| 79 | +## Complexity Analysis |
| 80 | + |
| 81 | +### Time Complexity: $O(1)$ |
| 82 | + |
| 83 | +> **Reason**: For arbitrary inputs, we do $O(w)$ work, where w is the number of bits in `n`. However, $w \leq 32$. |
| 84 | +
|
| 85 | +### Space Complexity: $O(1)$ |
| 86 | + |
| 87 | +> **Reason**: or alternatively $O(w)$. |
| 88 | +
|
| 89 | +### Approach 2: Divide By Two |
| 90 | +#### Algorithm |
| 91 | + |
| 92 | +We can get the last bit and the rest of the bits via `n % 2` and `n // 2` operations. Let's remember `cur`, the last bit of `n`. If the last bit ever equals the last bit of the remaining, then two adjacent bits have the same value, and the answer is `False`. Otherwise, the answer is `True`. |
| 93 | + |
| 94 | +Also note that instead of `n % 2` and `n // 2`, we could have used operators `n & 1` and `n >>= 1` instead. |
| 95 | + |
| 96 | +## Code in Different Languages |
| 97 | + |
| 98 | +<Tabs> |
| 99 | +<TabItem value="java" label="Java"> |
| 100 | + <SolutionAuthor name="@Shreyash3087"/> |
| 101 | + |
| 102 | +```java |
| 103 | +class Solution { |
| 104 | + public boolean hasAlternatingBits(int n) { |
| 105 | + int cur = n % 2; |
| 106 | + n /= 2; |
| 107 | + while (n > 0) { |
| 108 | + if (cur == n % 2) return false; |
| 109 | + cur = n % 2; |
| 110 | + n /= 2; |
| 111 | + } |
| 112 | + return true; |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +</TabItem> |
| 118 | +<TabItem value="python" label="Python"> |
| 119 | + <SolutionAuthor name="@Shreyash3087"/> |
| 120 | + |
| 121 | +```python |
| 122 | +class Solution(object): |
| 123 | + def hasAlternatingBits(self, n): |
| 124 | + n, cur = divmod(n, 2) |
| 125 | + while n: |
| 126 | + if cur == n % 2: return False |
| 127 | + n, cur = divmod(n, 2) |
| 128 | + return True |
| 129 | +``` |
| 130 | +</TabItem> |
| 131 | +</Tabs> |
| 132 | + |
| 133 | +## Complexity Analysis |
| 134 | + |
| 135 | +### Time Complexity: $O(1)$ |
| 136 | + |
| 137 | +> **Reason**: For arbitrary inputs, we do $O(w)$ work, where w is the number of bits in n. However, $w \leq 32$. |
| 138 | +
|
| 139 | +### Space Complexity: $O(1)$ |
| 140 | + |
| 141 | +> **Reason**: constant space is used. |
| 142 | +
|
| 143 | +## References |
| 144 | + |
| 145 | +- **LeetCode Problem**: [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/description/) |
| 146 | + |
| 147 | +- **Solution Link**: [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/solutions/) |
0 commit comments