You are given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [-1,0,2,4,6,8], target = 5
Output: 4Example 2:
Input: nums = [-1,0,2,4,6,8], target = 10
Output: 6Constraints:
1 <= nums.length <= 10,000.-10,000 < nums[i], target < 10,000nums contains distinct values sorted in ascending order.We scan the array from left to right looking for the first element that is greater than or equal to the target. If we find such an element, that index is where the target either exists or should be inserted. If no element qualifies, the target belongs at the end.
i in the array.nums[i] >= target, return i.n (the length of the array to insert at the end).Since the array is sorted, we can use binary search to find the target in logarithmic time. We track the potential insertion point as we search. Whenever we find an element greater than the target, we update our answer and continue searching left for a potentially smaller valid index.
res = n (default insertion point at the end) and pointers l = 0, r = n - 1.l <= r:mid = (l + r) / 2.nums[mid] == target, return mid.nums[mid] > target, set res = mid and search left with r = mid - 1.l = mid + 1.res (final insertion position).A cleaner observation: when binary search ends without finding the target, the left pointer l naturally lands on the correct insertion position. This happens because l always moves past elements smaller than the target, stopping exactly where the target should go.
l = 0 and r = n - 1.l <= r:mid = (l + r) / 2.nums[mid] == target, return mid.nums[mid] > target, search left with r = mid - 1.l = mid + 1.l as the insertion index (where l naturally lands on the correct position).This is the classic lower bound algorithm. We find the smallest index where the element is greater than or equal to the target. By using l < r as the condition and setting r = m when nums[m] >= target, we converge on the lower bound without needing a separate result variable.
l = 0 and r = n (note: r starts at n, not n - 1).l < r:m = l + (r - l) / 2.nums[m] >= target, set r = m.l = m + 1.l (the lower bound position).Most languages provide a built-in binary search or lower bound function. These functions return the index where the target is found or the position where it should be inserted to maintain sorted order. Using these avoids reimplementing binary search.
bisect_left in Python, lower_bound in C++, Arrays.binarySearch in Java).-index - 1).A frequent mistake is using incorrect loop conditions or boundary updates. Using l < r instead of l <= r (or vice versa) without adjusting the logic accordingly can cause the algorithm to miss elements or enter infinite loops. Similarly, setting r = mid instead of r = mid - 1 in certain variants can lead to incorrect results or infinite loops.
When the target is larger than all elements in the array, the insertion position should be n (the length of the array). Beginners often forget to account for this case, either returning -1, causing an index out of bounds error, or returning the last index incorrectly. Always ensure your algorithm correctly returns n when the target exceeds all array elements.