Given a positive integer n
, find and return the longest distance between any two adjacent 1
's in the binary representation of n
. If there are no two adjacent 1
's, return 0
.
Two 1
's are adjacent if there are only 0
's separating them (possibly no 0
's). The distance between two 1
's is the absolute difference between their bit positions. For example, the two 1
's in "1001"
have a distance of 3.
Example 1:
Input: n = 22 Output: 2 Explanation: 22 in binary is "10110". The first adjacent pair of 1's is "10110" with a distance of 2. The second adjacent pair of 1's is "10110" with a distance of 1. The answer is the largest of these two distances, which is 2. Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.
Example 2:
Input: n = 8 Output: 0 Explanation: 8 in binary is "1000". There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.
Example 3:
Input: n = 5 Output: 2 Explanation: 5 in binary is "101".
Constraints:
1 <= n <= 109
When 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 strategy is like painstakingly examining every possibility. We will convert the number to its binary representation and then explore every possible gap between ones to find the biggest one.
Here's how the algorithm would work step-by-step:
def binary_gap_brute_force(number):
binary_representation = bin(number)[2:]
maximum_gap = 0
current_gap = 0
found_first_one = False
for bit in binary_representation:
if bit == '1':
# If this is the second '1' found, compare gaps
if found_first_one:
maximum_gap = max(maximum_gap, current_gap)
current_gap = 0
else:
found_first_one = True
elif found_first_one:
# Only count zeros after the first '1'
current_gap += 1
return maximum_gap
The best way to find the biggest gap between ones in a binary number is to keep track of the positions of the ones. We'll scan the binary representation from left to right, remembering the last seen one, and calculating the gap each time we find a new one.
Here's how the algorithm would work step-by-step:
def binary_gap(number):
last_one_position = None
maximum_gap = 0
# Iterate through bits
for index in range(number.bit_length()):
# Check if the current bit is a 1
if (number >> index) & 1:
# First 1 found
if last_one_position is None:
last_one_position = index
else:
# Gap between current and last 1
current_gap = index - last_one_position
# Store the largest gap found so far
maximum_gap = max(maximum_gap, current_gap)
last_one_position = index
return maximum_gap
Case | How to Handle |
---|---|
N is 0 | Return 0 as the binary representation has no gaps. |
N is 1 | Return 0 as the binary representation has no gaps. |
N is a power of 2 (e.g., 2, 4, 8, 16) | Return 0 because there are no two '1's with a '0' in between. |
N has leading zeros in its binary representation | The algorithm will ignore leading zeros when searching for '1' bits. |
N has no gaps (e.g., 3, 7, 15) | Return 0 as there are no gaps between 1s. |
N is the maximum integer value (2147483647) | The algorithm should correctly handle the large number without integer overflow issues because it uses bitwise operations. |
N has a single gap | The algorithm correctly computes the length of this single gap. |
N has multiple gaps of varying lengths | The algorithm correctly identifies and returns the maximum gap length. |