Skip to content

Instantly share code, notes, and snippets.

@chunrapeepat
Last active April 19, 2020 12:33
Show Gist options
  • Save chunrapeepat/6f4395dc5afb96246e8ed5a874bea471 to your computer and use it in GitHub Desktop.
Save chunrapeepat/6f4395dc5afb96246e8ed5a874bea471 to your computer and use it in GitHub Desktop.
DAY5 - Binary Search - Find first number 1 in array
function findFirstOne(arr) {
let L = 0;
let R = arr.length - 1;
let result = -1;
while (L <= R) {
let mid = Math.floor(L + (R - L) / 2);
if (arr[mid] === 1) {
result = mid;
R = mid - 1;
} else {
L = mid + 1;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment