Back to Binary Search
Search Insert Position
Search Insert Position
š§© Problem Statement
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,3,5,6], target = 5
Output:
2
š¹ Example 2:
Input:
nums = [1,3,5,6], target = 2
Output:
1
š¹ Example 3:
Input:
nums = [1,3,5,6], target = 7
Output:
4
š Approaches
1. Binary Search
This is a variation of binary search where we want to find the first element >= target (lower_bound logic).
If we find nums[mid] == target, we return mid.
If the loop finishes without finding target, left will naturally point to the correction insertion position.
⨠Intuition
- Standard binary search finds an exact match.
- Here, we narrow down the range.
- When
left > right, the loop terminates. - At termination,
leftis the index of the first element greater than target, which is exactly where target should go.
š„ Algorithm Steps
- Initialize
left = 0,right = n - 1. - While
left <= right:
mid = left + (right - left) / 2.- If
nums[mid] == target, returnmid. - If
nums[mid] < target,left = mid + 1. - If
nums[mid] > target,right = mid - 1.
- Return
left.
ā³ Time & Space Complexity
- Time Complexity: $O(\log n)$.
- Space Complexity: $O(1)$.
š Code Implementations
C++
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
if (nums[mid] < target) left = mid + 1;
else right = mid - 1;
}
return left;
}
};
Python
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return left
Java
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
if (nums[mid] < target) left = mid + 1;
else right = mid - 1;
}
return left;
}
}
š Real-World Analogy
Library Bookshelf:
You have a row of books sorted by ID. You want to place a new book with ID X.
- You check the middle book. ID is smaller than X? Look right.
- ID is bigger than X? Look left.
- Eventually, you find a spot between two books (or at an end) where the left book is smaller and right book is bigger. That's your spot.
šÆ Summary
ā
O(log n): Required complexity.
ā
Return left: The key modification to standard binary search.
Solution Code
O(n) TimeO(1) Space
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int searchInsert(vector<int> &nums, int target) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target)
return mid;
if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
};
int main() {
Solution sol;
vector<int> nums = {1, 3, 5, 6};
cout << "Insert 5: " << sol.searchInsert(nums, 5) << endl; // Expected: 2
cout << "Insert 2: " << sol.searchInsert(nums, 2) << endl; // Expected: 1
cout << "Insert 7: " << sol.searchInsert(nums, 7) << endl; // Expected: 4
return 0;
}
SYSTEM STABLEUTF-8 ⢠STATIC_RENDER