Skip to content

Instantly share code, notes, and snippets.

@cfc1020
Created June 5, 2020 17:25
Show Gist options
  • Save cfc1020/1d02f4879c3f844534c1e7d5c3e40714 to your computer and use it in GitHub Desktop.
Save cfc1020/1d02f4879c3f844534c1e7d5c3e40714 to your computer and use it in GitHub Desktop.
Return the longest run of 1s for a given integer n's binary representation.
# @param {Integer} n
# @return {Integer}
def binary_gap(n)
res = 0
while n.nonzero? do
n &= n << 1
res += 1
end
res
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment