Advertisement

Binary Gap - LeetCode 868 Solution

Binary Gap - Complete Solution Guide

Binary Gap is LeetCode problem 868, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem requires finding the maximum distance between any two adjacent '1's in the binary representation of a given positive integer 'n'. The distance is defined as the number of '0's (or the number of bits) between the two '1's, plus 1 (or the difference in their indices). If there are fewer than two '1's in the binary representation, the function should return 0.

Solution Approach

The solution iterates through the binary representation of the integer. It maintains the index of the last encountered '1'. When it finds a '1', it calculates the distance from the current '1' to the last '1', and updates the maximum distance found so far. The distance is calculated as the difference between the current index and the index of the last '1'. After processing all bits, the maximum distance is returned.

Step-by-Step Algorithm

  1. Step 1: Convert the integer 'n' to its binary string representation (or directly process bits without explicit conversion).
  2. Step 2: Initialize variables: 'max_distance' to 0 (to store the maximum distance found so far), and 'last_one' to -1 (to indicate that no '1' has been found yet).
  3. Step 3: Iterate through the binary string (or bits of the integer).
  4. Step 4: In each iteration, check if the current bit is '1'.
  5. Step 5: If the current bit is '1', and 'last_one' is not -1 (meaning a previous '1' was found), calculate the distance between the current '1' and the previous '1' (distance = current_index - last_one).
  6. Step 6: Update 'max_distance' with the maximum of 'max_distance' and the calculated 'distance'.
  7. Step 7: Update 'last_one' to the current index.
  8. Step 8: After iterating through all the bits, return 'max_distance'.

Key Insights

  • Insight 1: Convert the integer to its binary string representation to easily identify '1's and their positions.
  • Insight 2: Iterate through the binary string (or the bits of the integer) to find all occurrences of '1's.
  • Insight 3: Keep track of the index of the last '1' encountered and compute the distance to the current '1'.

Complexity Analysis

Time Complexity: O(log n)

Space Complexity: O(log n)

Topics

This problem involves: Bit Manipulation.

Companies

Asked at: X, eBay.