Skip to content

Instantly share code, notes, and snippets.

@namtx
Created September 24, 2018 08:06
Show Gist options
  • Save namtx/9daba05317bcbb6734e1b1e7962f1000 to your computer and use it in GitHub Desktop.
Save namtx/9daba05317bcbb6734e1b1e7962f1000 to your computer and use it in GitHub Desktop.
func binaryGap(N int) int {
count := 0
max := 0
isPreviousOne := false
for N > 0 {
// fmt.Println(N % 2)
if N%2 == 1 {
if count > max {
max = count
}
count = 1
isPreviousOne = true
} else {
if isPreviousOne {
count++
}
}
N = N / 2
}
// fmt.Println(max)
return max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment