Last Updated: March 29, 2026
We have a sorted array of unique integers and a target value. If the target already exists in the array, return its index. If it doesn't, return the index where it would need to go to keep the array sorted.
This is really asking one question: what is the leftmost position where target could be placed without breaking the sorted order? If target matches an element, that element's index is the answer. If it doesn't, the answer is the index of the first element greater than target, or the array length if target is larger than everything.
The key observation is that this is exactly the "lower bound" problem from binary search. We're looking for the first index i where nums[i] >= target. That single framing handles both the "found" and "not found" cases in one shot.
1 <= nums.length <= 10^4 → The array has at most 10,000 elements. An O(n) scan would be fast enough in practice, but the problem explicitly requires O(log n).nums contains distinct values sorted in ascending order → Sorted + distinct is a strong signal for binary search. No duplicates means there's exactly one correct insert position.-10^4 <= target <= 10^4 → The target could be smaller than all elements (insert at index 0) or larger than all elements (insert at end). Both edge cases matter.The most straightforward approach: walk through the array from left to right and find the first element that is greater than or equal to the target. If we find such an element, its index is the insert position. If we reach the end without finding one, the target belongs at the end.
This is simple and correct, but it doesn't meet the O(log n) requirement. Still, it's worth understanding because it makes the binary search optimization feel natural. The linear scan checks every element one by one. Binary search does the same logical task but skips half the remaining elements at each step.
i from 0 to n - 1.nums[i] >= target, return i. This is either the target itself or the first element larger than it.n. The target is larger than every element.The linear scan works but doesn't meet the O(log n) requirement. Since the array is sorted, we can use binary search to eliminate half the search range at each step.
The array is sorted. That's the biggest clue. When you have a sorted array and need to find something in O(log n), binary search is the tool.
But here's the subtle part: we're not just searching for a target. We need to find the correct insert position, which means finding the first index where nums[i] >= target. This is called the "lower bound" in binary search terminology, and it handles both cases cleanly:
The standard binary search template for lower bound works like this: maintain a search window [left, right). At each step, check the middle element. If it's less than the target, the answer must be in the right half, so move left to mid + 1. If it's greater than or equal to the target, the answer could be mid itself or something to the left, so move right to mid. When the window collapses (left == right), left is the answer.
At every step, we maintain the invariant that the answer lies in the range [left, right]. When nums[mid] < target, every index from left to mid is too small, so we can safely move left to mid + 1. When nums[mid] >= target, mid itself could be the answer, so we set right = mid to keep it in the search range.
The loop terminates when left == right, and at that point, left is the smallest index where nums[i] >= target. If no such index exists (target is larger than everything), left equals nums.length, which is exactly where the target would be inserted.
left = 0 and right = nums.length.left < right:mid = left + (right - left) / 2 to avoid integer overflow.nums[mid] < target, set left = mid + 1.right = mid.left.