Binary search and lower-bound positions
Quick answer
Binary search repeatedly discards half of a sorted search interval. A lower-bound variant returns the first index whose value is at least the target, including the insertion position if it is absent.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
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));Expected console output:
1 3The first duplicate and insertion position are returned.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
function lowerBound(a,x) { let lo=0, hi=a.length; while(lo<hi) { const m=lo+Math.floor((hi-lo)/2); if(a[m]<x) lo=m+1; else hi=m; } return lo;}console.log(lowerBound([],9),lowerBound([2,6],8));Expected console output:
0 2Both are valid insertion positions even though there is no match.
How it works and cost model
Use a half-open interval [lo, hi), initialized to [0, length). Compute mid = lo + Math.floor((hi - lo) / 2). If values[mid] < target, advance lo to mid + 1; otherwise set hi = mid. The invariant is that every index before lo is too small and every index at or after hi is at least target. When lo equals hi, the position is determined. A follow-up equality check distinguishes a found item from an insertion position. For random access and bounded comparisons, this takes O(log n) comparisons for nonempty input and O(1) auxiliary space. Do not sort the data as part of every lookup without accounting for sorting cost.
Common mistakes
Never apply the invariant to unsorted data. Avoid 32-bit bitwise midpoint tricks for general array indices; also handle the position equal to length.
Practice
Implement lowerBound for sorted numeric arrays and use it to find the first occurrence of 4 in [1,4,4,7]. Test empty, before-first and after-last targets.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Binary search and lower-bound positions — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
What property allows the lower-bound loop to discard half the interval?
When lowerBound returns a.length, what does that mean?
Why use hi = mid rather than hi = mid - 1 in this half-open lower-bound version?
What is the exact console output in the Lower-bound binary search example 1?
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));What is the exact console output in the Lower-bound binary search example 2?
function lowerBound(a,x) { let lo=0, hi=a.length; while(lo<hi) { const m=lo+Math.floor((hi-lo)/2); if(a[m]<x) lo=m+1; else hi=m; } return lo;}console.log(lowerBound([],9),lowerBound([2,6],8));