Skip to content

Instantly share code, notes, and snippets.

@cardotrejos
Created June 5, 2020 20:54
Show Gist options
  • Save cardotrejos/e8a55d02d2338b733a5e4e6d697b8bac to your computer and use it in GitHub Desktop.
Save cardotrejos/e8a55d02d2338b733a5e4e6d697b8bac to your computer and use it in GitHub Desktop.
Function def binary_gap(n) that, given a positive integer N, returns the length of its longest binary gap.
def bin_gap (n)
bin = n.to_s(2)
temp = []
gaps = []
bin.each_char do |char|
if char == "1"
unless temp.empty?
gaps << temp.length
temp = []
end
else
temp << char
end
end
return 0 if gaps.empty?
gaps.max
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment