868-Binary-Gap

Sat 17 May 2025

https://leetcode.com/problems/binary-gap

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def binaryGap(self, n: int) -> int:
    ans = 0
    d = -32  # Distance between any two 1's, initialized to a reasonable small value

    while n:
      if n & 1:
        ans = max(ans, d)
        d = 0
      n //= 2
      d += 1

    return ans
new Solution().binaryGap()

Score: 5

Category: leetcode