899. Binary Gap
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the longest distance between any two "adjacent" 1s in the binary representation of a positive integer n. Two 1s are considered adjacent if they are separated only by zeros. The distance is the difference in their bit positions. If n's binary form has fewer than two 1s, the distance is 0.
Solution
Binary Gap in Python
class Solution:
def binaryGap(self, n: int) -> int:
# Position of the last '1' found. Initialized to -1 to indicate no '1's found yet.
last_one_pos = -1
# The maximum distance found so far.
max_distance = 0
# The current bit position, starting from 0 (rightmost bit).
current_pos = 0
while n > 0:
# Check if the last bit (least significant bit) is 1.
if (n & 1) == 1:
# If we have seen a '1' before (i.e., this is not the first '1').
if last_one_pos != -1:
# Calculate the distance to the last '1' and update the maximum distance.
max_distance = max(max_distance, current_pos - last_one_pos)
# Update the position of the last '1' to the current position.
last_one_pos = current_pos
# Move to the next bit by right shifting.
n >>= 1
# Increment the current position.
current_pos += 1
return max_distance
Binary Gap in Java
class Solution {
public int binaryGap(int n) {
// Position of the last '1' found. Initialized to -1 to indicate no '1's found yet.
int lastOnePos = -1;
// The maximum distance found so far.
int maxDistance = 0;
// The current bit position, starting from 0 (rightmost bit).
int currentPos = 0;
while (n > 0) {
// Check if the last bit (least significant bit) is 1.
if ((n & 1) == 1) {
// If we have seen a '1' before (i.e., this is not the first '1').
if (lastOnePos != -1) {
// Calculate the distance to the last '1' and update the maximum distance.
maxDistance = Math.max(maxDistance, currentPos - lastOnePos);
}
// Update the position of the last '1' to the current position.
lastOnePos = currentPos;
}
// Move to the next bit by right shifting.
n >>= 1;
// Increment the current position.
currentPos++;
}
return maxDistance;
}
}
Binary Gap in C++
class Solution {
public:
int binaryGap(int n) {
// Position of the last '1' found. Initialized to -1 to indicate no '1's found yet.
int last_one_pos = -1;
// The maximum distance found so far.
int max_distance = 0;
// The current bit position, starting from 0 (rightmost bit).
int current_pos = 0;
while (n > 0) {
// Check if the last bit (least significant bit) is 1.
if ((n & 1) == 1) {
// If we have seen a '1' before (i.e., this is not the first '1').
if (last_one_pos != -1) {
// Calculate the distance to the last '1' and update the maximum distance.
max_distance = std::max(max_distance, current_pos - last_one_pos);
}
// Update the position of the last '1' to the current position.
last_one_pos = current_pos;
}
// Move to the next bit by right shifting.
n >>= 1;
// Increment the current position.
current_pos++;
}
return max_distance;
}
};
Binary Gap in TypeScript
function binaryGap(n: number): number {
// Position of the last '1' found. Initialized to -1 to indicate no '1's found yet.
let lastOnePos: number = -1;
// The maximum distance found so far.
let maxDistance: number = 0;
// The current bit position, starting from 0 (rightmost bit).
let currentPos: number = 0;
while (n > 0) {
// Check if the last bit (least significant bit) is 1.
if ((n & 1) === 1) {
// If we have seen a '1' before (i.e., this is not the first '1').
if (lastOnePos !== -1) {
// Calculate the distance to the last '1' and update the maximum distance.
maxDistance = Math.max(maxDistance, currentPos - lastOnePos);
}
// Update the position of the last '1' to the current position.
lastOnePos = currentPos;
}
// Move to the next bit by right shifting.
n >>= 1;
// Increment the current position.
currentPos++;
}
return maxDistance;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.