Ctrl K

Widest Gap Between Ones

Easy
You are given a positive integer number. Write it in binary and look at every pair of **neighbouring** ones, meaning ones with only zeros (or nothing) between them. Their distance is the difference of their positions: in 1001, for example, the ones are 3 apart. Return the largest such distance. If the binary form has just one 1, return 0.
Example 1
number=41 → 3

41 in binary is 101001. Neighbouring ones are 2 and 3 positions apart, and the largest is 3.

Example 2
number=16 → 0

16 is 10000 with a single one, so there is no pair and the answer is 0.

Example 3
number=7 → 1

7 is 111, all ones sit side by side, so the distance is 1.

Constraints
  • 1 ≤ number ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyThe Unpaired Value EasyFlip the Bit Order EasyCount the Set Bits EasySize of a Packed Tree Easy
loading Python…
Input
number=41
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 52 tests on the server, hidden ones included.