Concepts

Lower-bound binary search

Try the free JavaScript compiler

Find the first position with value at least the target in an ascending array; may return length. A membership check is separate.

Formal Syntax & Structure

javascript syntaxjavascript
lowerBound(sortedValues, target) → insertion index

Working Example

Example Snippetjavascript
function lowerBound(a, x) {  let lo = 0, hi = a.length;  while (lo < hi) {    const mid = lo + Math.floor((hi - lo) / 2);    if (a[mid] < x) lo = mid + 1; else hi = mid;  }  return lo;}console.log(lowerBound([1,4,4,7],4), lowerBound([1,4,4,7],5));
JavaScript Reference