diff --git a/dsa-problems/leetcode-problems/0600-0699.md b/dsa-problems/leetcode-problems/0600-0699.md index fd7ca7675..6bf7bf55a 100644 --- a/dsa-problems/leetcode-problems/0600-0699.md +++ b/dsa-problems/leetcode-problems/0600-0699.md @@ -573,7 +573,7 @@ export const problems = [ "problemName": "693. Binary Number with Alternating Bits", "difficulty": "Easy", "leetCodeLink": "https://leetcode.com/problems/binary-number-with-alternating-bits", - "solutionLink": "#" + "solutionLink": "/dsa-solutions/lc-solutions/0600-0699/binary-number-with-alternating-bits" }, { "problemName": "694. Number of Distinct Islands", diff --git a/dsa-solutions/lc-solutions/0600-0699/0693-binary-number-with-alternating-bits.md b/dsa-solutions/lc-solutions/0600-0699/0693-binary-number-with-alternating-bits.md new file mode 100644 index 000000000..4da1f13ca --- /dev/null +++ b/dsa-solutions/lc-solutions/0600-0699/0693-binary-number-with-alternating-bits.md @@ -0,0 +1,147 @@ +--- +id: binary-number-with-alternating-bits +title: Binary Number with Alternating Bits +sidebar_label: 0693 - Binary Number with Alternating Bits +tags: + - Bit Manipulation + - String + - Bitmask +description: "This is a solution to the Binary Number with Alternating Bits problem on LeetCode." +--- + +## Problem Description + +Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. + +### Examples + +**Example 1:** + +``` +Input: n = 5 +Output: true +Explanation: The binary representation of 5 is: 101 +``` + +**Example 2:** + +``` +Input: n = 7 +Output: false +Explanation: The binary representation of 7 is: 111. +``` + +### Constraints + +- $$1 \leq n \leq 2^{31} - 1$$ + +## Solution for Binary Number with Alternating Bits + +### Approach 1: Convert to String +#### Intuition and Algorithm + +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. + +## Code in Different Languages + + + + + +```java +class Solution { + public boolean hasAlternatingBits(int n) { + String bits = Integer.toBinaryString(n); + for (int i = 0; i < bits.length() - 1; i++) { + if (bits.charAt(i) == bits.charAt(i+1)) { + return false; + } + } + return true; + } +} +``` + + + + + +```python +class Solution(object): + def hasAlternatingBits(self, n): + bits = bin(n) + return all(bits[i] != bits[i+1] + for i in xrange(len(bits) - 1)) +``` + + + +## Complexity Analysis + +### Time Complexity: $O(1)$ + +> **Reason**: For arbitrary inputs, we do $O(w)$ work, where w is the number of bits in `n`. However, $w \leq 32$. + +### Space Complexity: $O(1)$ + +> **Reason**: or alternatively $O(w)$. + +### Approach 2: Divide By Two +#### Algorithm + +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`. + +Also note that instead of `n % 2` and `n // 2`, we could have used operators `n & 1` and `n >>= 1` instead. + +## Code in Different Languages + + + + + +```java +class Solution { + public boolean hasAlternatingBits(int n) { + int cur = n % 2; + n /= 2; + while (n > 0) { + if (cur == n % 2) return false; + cur = n % 2; + n /= 2; + } + return true; + } +} +``` + + + + + +```python +class Solution(object): + def hasAlternatingBits(self, n): + n, cur = divmod(n, 2) + while n: + if cur == n % 2: return False + n, cur = divmod(n, 2) + return True +``` + + + +## Complexity Analysis + +### Time Complexity: $O(1)$ + +> **Reason**: For arbitrary inputs, we do $O(w)$ work, where w is the number of bits in n. However, $w \leq 32$. + +### Space Complexity: $O(1)$ + +> **Reason**: constant space is used. + +## References + +- **LeetCode Problem**: [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/description/) + +- **Solution Link**: [Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/solutions/)