EasyBinary Search

Search Insert Position โ€” Solution

Problem

Given a sorted array of distinct integers and a target value, find and return the target's index. If the target is not in the array, return the index where it would need to be inserted to keep the array sorted.

  • Input: nums = [1, 3, 5, 6], target = 5
  • Output: 2
  • Explanation: 5 exists at index 2.

When the target is absent:

  • Input: nums = [1, 3, 5, 6], target = 2
  • Output: 1
  • Explanation: 2 would be inserted between 1 and 3, so the answer is index 1.

Intuition

Because the array is sorted, we can eliminate half the remaining candidates each step โ€” the classic binary search setup. The insight that makes this problem clean is that when the search loop ends, the left pointer is sitting exactly at the insertion point: every move either pushed left right past a too-small value or pulled right left past a too-large one, so when they cross, left is the smallest index where the value would be โ‰ฅ target.

Approach 1 โ€” Linear Scan

Walk the array left to right and return the first index where the value is at least the target. This is easy to reason about but does not exploit the sorted order.

  1. Iterate through nums with index tracking.
  2. As soon as nums[index] >= target, return index.
  3. If the loop finishes, the target is larger than every element โ€” return len(nums).
1def searchInsert(self, nums: list[int], target: int) -> int:
2    for index, value in enumerate(nums):
3        if value >= target:
4            return index
5    return len(nums)  # target belongs after every element

Time: O(n) โ€” may scan every element before finding the position.
Space: O(1) โ€” no extra storage.

Approach 2 โ€” Binary Search

Maintain an inclusive [left, right] window and halve it each step. When the window closes (left > right), left is the insertion point.

  1. Initialize left = 0, right = len(nums) - 1.
  2. While left <= right, compute mid = left + (right - left) // 2.
  3. If nums[mid] == target, return mid immediately.
  4. If nums[mid] < target, the answer is in the right half: left = mid + 1.
  5. If nums[mid] > target, the answer is in the left half: right = mid - 1.
  6. Return left โ€” the loop invariant guarantees it is the insertion position.
1def searchInsert(self, nums: list[int], target: int) -> int:
2    left, right = 0, len(nums) - 1
3    while left <= right:
4        mid = left + (right - left) // 2
5        if nums[mid] == target:
6            return mid
7        elif nums[mid] < target:
8            left = mid + 1   # target must be in the right half
9        else:
10            right = mid - 1  # target must be in the left half
11    return left  # when left > right, left is the insertion point

Time: O(log n) โ€” search space halves on every iteration.
Space: O(1) โ€” only two index variables needed.

Complexity Summary

ApproachTimeSpaceWhen to use
Linear ScanO(n)O(1)Tiny inputs or as a baseline to verify binary search output
Binary SearchO(log n)O(1)Any sorted array โ€” the intended solution for this problem

Common Mistakes

  • Returning left - 1 when not found โ€” the loop ends with left at the correct insertion index; subtracting one shifts it to the wrong slot.
  • Integer overflow in the midpoint formula โ€” (left + right) / 2 overflows when both indices are near INT_MAX in Java and C++; always use left + (right - left) / 2.
  • Using strict > instead of >= in the linear scan โ€” nums[i] > target would skip the case where the target already exists in the array.
  • Forgetting the post-loop return len(nums) in the linear approach โ€” when the target is larger than every element, the loop exits without returning, so the explicit return n is required.
  • Mixing bound conventions mid-function โ€” for example, initializing right = len(nums) (exclusive) but writing nums[mid] > target โ†’ right = mid - 1 (inclusive update) mixes the two conventions and produces off-by-one errors; stay with inclusive bounds throughout.

Related Problems

Ready to practice? Try it on SkillFlow

Adaptive problems, AI follow-up interviews, and a skill score that shows exactly where you need to improve.

Practice This Problem โ†’