1. Exclusive right
right starts one past the last valid index.
let right = arr.lengthTyporithm · medium
Step 1 of 8 · intro card
Search Algorithms
Lower Bound Search finds the first index where a sorted array value is greater than or equal to target.
Lower bound is a Binary Search variant for insertion points and first matches.
It uses a half-open range: left is inclusive, right is exclusive.
When arr[mid] is too small, move left past mid.
Otherwise keep mid because it might be the first valid answer.
The returned index can equal arr.length when all values are smaller than target.
Find first value >= target.
Use right = arr.length.
Loop while left < right.
Keep mid when it could be the answer.
Return an insertion index.
function lowerBound(arr, target) {
let left = 0;
let right = arr.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}Connect each part of the code to the concept before you type.
right starts one past the last valid index.
let right = arr.lengthKeep narrowing until left is the first valid insertion point.
while (left < right)Probe the middle of the current half-open range.
const mid = Math.floor((left + right) / 2)If mid is too small, the lower bound must be to the right.
arr[mid] < targetDiscard mid and everything before it.
left = mid + 1mid could be the first value >= target, so keep it.
right = midleft is the first index whose value is not smaller than target.
return leftRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputArray [1,2,2,2,5,8,13] · target 9
Follow temporary values, local state and supporting structures at this step.
Local state · indices and saved values
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state