Search Insert Position
L5 Medium Binary Search
The same binary search that finds a value can find the spot where it would be inserted: when you miss, your final "low" pointer is the correct insertion index.
Given a sorted array of distinct integers nums and a target, return the index where target would be, keeping the order sorted if it were inserted. If target is already present, return its index.
Examples
▸ nums = [1, 3, 5, 6], target = 5
→ 2
▸ nums = [1, 3, 5, 6], target = 2
→ 1
▸ nums = [1, 3, 5, 6], target = 7
→ 4