Search Insert Position

Question (LC.35)

Given a sorted array and a target, return the index of the target if it exists or return the index in which target should be inserted.

Example

I: [1,3,5,6], 5
O: 2
I: [1,3,5,6], 2
O: 1

Linear Approach

Scan through the array linearly, find the target or a value bigger than the target

public int searchInsert(int[] nums, int target) {
    // good practice to check null or empty input
    if (nums == null || nums.length == 0) {
        return 0;
    }
    // linear scan
    int i;
    for (i = 0; i < nums.length; i++) {
        if (nums[i] >= target) {
            return i;
        }
    }
    return i;
}

This definite works. Can we do better?

Alternative Approach

Construct a binary search tree then search for the lower bound.

Binary Search Example

Consider this example, given input [0,2,3,4,6,7,8,11] find target 6 with the search function below.

Tracing through this example

If we visualize this array as a binary search tree diagram,

the worst case of going down a path is log(n), where n is the size of the input array. Because the binary tree is balanced and we have 2^height = n, height=log2(n)=O(log(n))height = log_2(n) = O(log(n)).

Recursive Implementation

A recursive implementation is intuitively sounding because we are applying the same search function either on the left or the right section.

The stopping condition is doing a bit extra work here. We will see later that this method can be more convenient in other cases. For this question, we have three possible cases 1. target < A[start], return start 2. A[start] < target < A[end], return end 3. A[end] < target < A[end+1], return end + 1

Iterative Implementation

The recursion only keeps track of start index and end index. We can have two variables outside of the while loop to accomplish the same functionality.

Time & Space Complexity

Recursive - O(logn) time (logn) space Iterative - O(logn) time O(1) space

The worst case of searching is going down a full path in the tree diagram above. The length of the full path or equivalently the height of the balanced tree is O(logn).

More formally, we can justify the runtime by solving the recurrence T(n) = T(n/2) + 1.

Follow Up

What if we allow duplicates in the input array?

Last updated