Modify the binary search algorithm so that it returns an array of length 2 with the lowest

Question:

Modify the binary search algorithm so that it returns an array of length 2 with the lowest index and highest index of those element(s) that equal the searched value.

For example, when searching for 3, if the array contains the values 1 1 3 3 3 4 5 5, the algorithm should return [2, 4].

If the value is not found, return an array of length 1 containing the index at which the value should be inserted.

Your implementation should have O(log n) running time even if most elements of the array have the same value.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  answer-question
Question Posted: