Skip to content

Instantly share code, notes, and snippets.

@kirylrb
Last active April 19, 2020 12:15
Show Gist options
  • Save kirylrb/eb91be434dadaca7f45096d71a9da8e8 to your computer and use it in GitHub Desktop.
Save kirylrb/eb91be434dadaca7f45096d71a9da8e8 to your computer and use it in GitHub Desktop.
Binary Gap in Ruby
# A binary gap within a positive integer N is any maximal sequence of consecutive zeros
# that is surrounded by ones at both ends in the binary representation of N.
def solution(n)
num_bin = n.to_s(2).tr('^0-9', '')
return 0 if !num_bin.include?("0") || num_bin.chars.last.eql?("0")
num_bin.scan(/0+/).max.size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment