LeetCode 868, Easy. Topics: Bit Manipulation. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:
Problem
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.
Examples
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.
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.
Explanation: 5 in binary is "101".
Constraints
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026