AlgoMaster Logo

Search Insert Position

easy5 min readUpdated June 23, 2026

Understanding the Problem

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.

The question reduces to: 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.

This is the "lower bound" problem from binary search: find the first index i where nums[i] >= target. That single framing handles both the "found" and "not found" cases.

Key Constraints:

  • 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.

Approach 1: Linear Scan

Intuition

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 correct but does not meet the O(log n) requirement. It does define the target the binary search will optimize: the linear scan checks every element one by one, while binary search performs the same logical task and discards half the remaining range at each step.

Algorithm

  1. Iterate through the array with index i from 0 to n - 1.
  2. If nums[i] >= target, return i. This is either the target itself or the first element larger than it.
  3. If the loop finishes without returning, return n. The target is larger than every element.

Example Walkthrough

1Start scan: i=0, looking for first element >= 2
0
1
i
1
3
2
5
3
6
1/4

Code

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.

Approach 2: Binary Search

Intuition

The array is sorted, which is what makes O(log n) possible. Binary search finds an element in a sorted array by discarding half the range at each step.

The task is not an exact-match search. We need the correct insert position, which is the first index where nums[i] >= target. This is the "lower bound", and it handles both cases:

  • If the target exists, the lower bound is the target's index.
  • If the target doesn't exist, the lower bound is where it would be inserted.

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.

Algorithm

  1. Set left = 0 and right = nums.length.
  2. While left < right:
    • Compute mid = left + (right - left) / 2 to avoid integer overflow.
    • If nums[mid] < target, set left = mid + 1.
    • Otherwise, set right = mid.
  3. Return left.

Example Walkthrough

1Initialize: left=0, right=4, search range = entire array
0
1
left
1
3
2
5
3
6
search range
1/7

Code