Search Insert Position

Posted by Bill on March 11, 2023

Search Insert Position

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.

1
2
3
4
5
6
7
8
9
10
11
12
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

C++ Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int searchInsert(vector<int>& nums, int target) {
    int left = 0;
    int right = nums.size() - 1;
    while (left +1 < right) {
        int mid = (left + right) / 2;
        int val = nums[mid];
        if (val == target) return mid;
        else if (target < val && mid > 0) {
            right = mid - 1;
        } else if (target > val) {
            left = mid + 1;
        }
    }
    if (nums[left] < target && nums[right] > target){
        return left + 1;
    } else if (nums[left] == target) {
        return left;
    } else if (nums[right] == target) {
        return right;
    }  else if(nums[left] > target) {
        return left;
    } else {
        return right + 1;
    }
}