Leetcode 35

Code4Good
Feb 14, 2021

Search Insert Position

Problem:

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.

https://leetcode.com/problems/search-insert-position/

Thought Process:

To go through the entire array, and come up with the following conditions:

If the target is the element at that index, then return the index

If the element is in between the current and the next element, then return index + 1 since the target is bigger than the current element

Some end cases: If there are zero elements, return 0. If there is one element, return 0 or 1 depending on if target is greater, smaller or equal to the element.

Code:

Works!

--

--