22. Search Insert Position

nums is sorted with distinct values. Return the index of target if found; otherwise return the index where it would be inserted to keep the order.

Example 1

Input: nums = [1,4,6,9], target = 6
Output: 2

Example 2

Input: nums = [1,4,6,9], target = 5
Output: 2
Hint 1

You are looking for the first index whose value is ≥ target (the “lower bound”).

Binary Search