Last active
April 19, 2020 12:33
-
-
Save chunrapeepat/6f4395dc5afb96246e8ed5a874bea471 to your computer and use it in GitHub Desktop.
DAY5 - Binary Search - Find first number 1 in array
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 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