Last active
May 9, 2020 02:31
-
-
Save jerojasro/6706779bf2e865221d9699da50c6017d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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