Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.
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.
Example 3:
Input: n = 11 Output: false Explanation: The binary representation of 11 is: 1011.
Constraints:
1 <= n <= 231 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach checks every single digit in the number one at a time. We compare each digit with the one right before it to see if they alternate.
Here's how the algorithm would work step-by-step:
def has_alternating_bits_brute_force(number):
binary_representation = bin(number)[2:]
number_of_bits = len(binary_representation)
# Need at least 2 bits to have alternating bits
if number_of_bits < 2:
return True
for index in range(1, number_of_bits):
# Compare current bit to the previous bit
if binary_representation[index] == binary_representation[index - 1]:
# If the bits are equal, it's not alternating
return False
return TrueThe problem asks us to check if a number's binary representation has alternating bits (0101 or 1010). The clever approach involves recognizing a pattern in the binary number itself to efficiently confirm this alternation, avoiding complex bit-by-bit comparisons.
Here's how the algorithm would work step-by-step:
def has_alternating_bits(number):
shifted_number = number >> 1
# XOR to highlight differences between adjacent bits.
xor_result = number ^ shifted_number
added_result = xor_result + 1
# Power of 2 check confirms alternating pattern.
return (added_result & (added_result - 1)) == 0| Case | How to Handle |
|---|---|
| Input is zero (0) | Zero is not considered to have alternating bits, so return false. |
| Input is one (1) | One is considered to have alternating bits, so return true. |
| Large integer inputs close to the maximum integer value. | Ensure the bitwise operations or string conversions used in the solution do not cause integer overflow leading to incorrect results. |
| Integer with all 1s (e.g., 2^n - 1) as input | This should return false, because all bits are 1s, and it doesn't have alternating bits. |
| Powers of 2 as inputs (e.g., 2, 4, 8, 16...) | Check for this explicitly as they contain only one set bit, therefore do not have alternating bits. |
| Negative input. | The problem statement does not specify the behavior for negative numbers, clarify that negative numbers are invalid inputs and should be rejected. |
| Integer with alternating bits starting with 0 (e.g., 42 (101010)) | The algorithm must correctly identify alternating patterns regardless of whether they start with 0 or 1. |
| Integer with repeating sequences of bits like 110011 or 100100 | Ensure the algorithm does not incorrectly identify these as alternating since alternating needs to be between adjacent bits. |