Search Insertion Position
Published in
Mar 4, 2022
Given a sorted array of distinct integers and a target value, how could we get the index of the target is found? In addition, if the target is not found, we need to return the index where it would be if it were inserted in order.
The first solution came to my mind is a recursive one. Oh my…
We can further reduce the number of base cases to make our recursion code shorter, as demonstrated below.
Based on this, we can change it to be iterative as follows.