ASAPUtils Logo ASAPUtils
Week 4

Binary Search

Binary Search explained through the active-interval invariant, with an overflow-safe midpoint, C++ and JavaScript implementations, complexity, off-by-one traps, and an interactive elimination trace.

easy Arrays Binary Search O(log n) time · O(1) space Open on LeetCode ↗

The problem

Given a sorted array of distinct integers and a target, return the target's index or minus one if it is absent. The required running time is logarithmic.

nums = [-1,0,3,5,9,12], target = 9 -> 4

Related Problems