Skip to content

Instantly share code, notes, and snippets.

@jerojasro
Last active May 9, 2020 02:31
Show Gist options
  • Save jerojasro/6706779bf2e865221d9699da50c6017d to your computer and use it in GitHub Desktop.
Save jerojasro/6706779bf2e865221d9699da50c6017d to your computer and use it in GitHub Desktop.
def binary_gap(n):
gap_len = 0
best_gap = 0
i = 0
gap_started = False
while n > 0:
if n & 1:
gap_started = True
if gap_len >= 1 and gap_len > best_gap:
best_gap = gap_len
gap_len = 0
elif gap_started:
gap_len = gap_len + 1
n = n >> 1
return best_gap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment