Created
June 5, 2020 17:25
-
-
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.
This file contains hidden or 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
# @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