Last active
October 1, 2017 18:03
-
-
Save johndavedecano/a61cae469f187edfaa20a7e74005785a to your computer and use it in GitHub Desktop.
EXAMS - Binary Gap
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
function solution(N) { | |
// write your code in JavaScript (Node.js 6.4.0) | |
var str = (N >>> 0).toString(2).split(''); | |
var items = []; | |
var flag = false; | |
for (var i=0; i<str.length; i++) { | |
if (str[i] !== "0") { | |
flag = false; | |
} else if (str[i] === "0" && flag === false) { | |
items[items.length] = "0"; | |
flag = true; | |
} else if (str[i] === "0" && flag === true) { | |
items[items.length - 1] = items[items.length - 1] + "0"; | |
} | |
} | |
if (items.length === 0) { | |
return 0; | |
} | |
return items.sort((a, b) => b.length - a.length)[0].length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment