Math Fundamentals18 sections · 814 units
Open in Course

Search Insert - The Insight (Binary search variant)

Find or insert position

Binary search normally finds an exact match. Here, you want the position where the target belongs, even if it's not in the array.

The trick: when binary search ends without finding the target, the left pointer ll points to the insertion position. Why? Because ll is the first index where nums[l]targetnums[l] \geq target.

This is a common pattern: binary search returns not just exact matches, but also boundary positions. You'll use this in many problems.