Created
February 10, 2018 18:03
-
-
Save mayashavin/f23981ec4a29b02a4461856e705addbb to your computer and use it in GitHub Desktop.
This file contains 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 firstBadVersion(arr){ | |
let found = undefined, start = 0, end = arr.length; | |
while (start < end && !found){ | |
let mid = Math.floor((start + end)/2); | |
if (isBad(arr[mid])){ | |
if (arr[mid - 1] && isBad(arr[mid - 1])){ | |
end = mid - 1; | |
} | |
else{ | |
found = mid; | |
} | |
} | |
else{ | |
start = mid + 1; | |
} | |
} | |
return found; | |
} | |
function isBad(version){ | |
return version === 'B'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment