Search Insert Position
Problem
nums of distinct integers and a
value target. Return the index of target if it is present;
otherwise return the index where it would have to be inserted so the array stays sorted.
Aim for O(log n) time.nums = [1,3,5,6], target = 5Output2nums = [1,3,5,6], target = 2Output1nums = [1,3,5,6], target = 7Output41 ≤ n ≤ 104 · -104 ≤ nums[i], target ≤ 104 · nums sorted ascending, distinct · O(log n) required
Try it yourself
class Solution {
public int searchInsert(int[] nums, int target) {
}
}
Instinct Sorted data, a rotated array, or find the min/max X that works where a yes/no test flips only once - halve the search space every step.
Memory hack Find the fence - every candidate reads NO NO NO YES YES, and binary search just walks to where the fence stands.
Open the Binary Search guide →Approaches
Brute ForceLinear Scan for First ≥ target
O(n)Space O(1)Walk left to right and stop at the first element that is not smaller than
target — that is either the match or the slot it belongs in. If no
such element exists, the target goes after everything, at index n.
public int searchInsert(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { if (nums[i] >= target) return i; // first slot that can hold target } return nums.length; // target is larger than every element }
Correct and tiny, but ignores sortedness and misses the O(log n) requirement.
OptimalBinary Search on the Lower Bound
O(log n)Space O(1)Keep a half-open window [lo, hi) that always contains the answer,
starting with hi = n so “insert at the end” is reachable. If
nums[mid] < target, mid and everything left of it are too small;
otherwise mid might be the answer, so keep it. When the window closes, lo
is the first index with value ≥ target.
public int searchInsert(int[] nums, int target) { int lo = 0, hi = nums.length; // answer always lies in [lo, hi] while (lo < hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] < target) lo = mid + 1; // mid and its left side are too small else hi = mid; // mid may be the answer, keep it } return lo; }
Dry runnums = [1,3,5,6], target = 5
- lo=0 hi=71lo356m810126 < 7 → lo=4
- lo=4 hi=713568lo10m1210 ≥ 7 → hi=5
- lo=4 hi=513568lo m10hi128 ≥ 7 → hi=4
- lo=hi=413568lo hi1012insert 7 at 4 ✔
Logarithmic, no special cases — the same template solves every lower-bound question.
hi = n (not n - 1) so the “bigger than
everything” case needs no extra check after the loop.At a glance
| Level | Approach | Time | Space |
|---|---|---|---|
| Brute Force | Linear Scan for First ≥ target | O(n) | O(1) |
| Optimal | Binary Search on the Lower Bound | O(log n) | O(1) |
- Starting with
hi = n - 1and forgetting the “insert at the end” case. - Writing
hi = mid - 1in the lower-bound template — can skip the answer. - Computing
(lo + hi) / 2carelessly; preferlo + (hi - lo) / 2.
- Reframe both cases as “first index with value ≥ target” before coding.
- State the loop invariant: the answer always stays inside
[lo, hi]. - Mention that Java’s
Arrays.binarySearchencodes the insert point as-(pos) - 1.