Easy · Binary Search
Understand → Visualise → Practice.
Sorted order tells you which half cannot contain the target. Repeated halving turns a linear search into a logarithmic one.
Maintain an inclusive search range [lo, hi], compare its middle value, then discard the impossible half.
mid = lo + (hi − lo) // 2.
Equal returns; smaller moves lo; larger moves hi.
lo > hi means no positions remain.
O(log n) time and O(1) space.
Use `while lo <= hi`; using `<` can skip the final candidate.
Read the full guides first: Sorting Algorithms and Their Trade-offs, Binary Search and Boundary Finding.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.