### Question

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.

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

### Challenges

This task looks very simple, and at a first glance, it could be solved with the indexOf method. But this condition makes it trickier — “If the target isn’t found, return the index where it would be if it were inserted in order.”

So let’s break it down.

1. Compare each element of nums array to target
2. if matches return the index of matching element
3. if the element doesn’t match, check if the next element is less than the target if it’s not — return the next index position as a result.
4. Check if reached the end of the array and return position

### Let’s code

For the setup, I will need 2 variables:

pos — position, index of array elements

result — record the final result

`let pos = 0;`
`let result = 0;`

Now let’s check if we even need to dig into our array because if the first element of the array is more than the target, then the target element should have an index of 0. So we check it and return the result that is 0.

`if (nums[0]<target) {`
`  //* further calculations will be here`
`}`
`return result;`

It’s time to build our recursive function, somehow I’ve been using them a lot. My find function will take 3 arguments:

1. nums — the original array
2. target — the original target
3. pos — index/position of array elements

It will be if statement with the following steps. First, let’s compare the element of the array with the target. If we found a match, our result should be equal to the current pos.

`if (nums[pos]===target) {`
`  result = pos;`
`}`

Otherwise, I would use else-if with or condition. If the next element of the array is more than the target, it means that the target should take the position of the next element OR if we’ve reached the end of the array and didn’t find a match, the target should go to the end of the array. Therefore both of those scenarios would yield the same result.

`else if (nums[pos+1]> target || pos+1===nums.length) {`
`  result = pos+1;`
`}`

Lastly, if none of the above conditions worked we will move to the next element of the array and execute find with an updated pos argument.

`else {`
`  pos++;`
`  find(nums,target,pos)`
`}`

This was a really fun exercise, but I found an even faster solution.

`var searchInsert = function(nums, target) {`
`  let newArray = […nums, target].sort((a, b) => {return a — b})`
`  const position = newArray.indexOf(target)`
`  return position`
`};`

Wow, that’s was my thought. Simple like 1, 2, 3.

1. Create a new array by adding target to a new array
2. Sort newly created array
3. I mentioned indexOf method earlier, well now we can use it and return the result.

### Code

I somehow got stuck on recursive functions, but sometimes there are way easier solutions. In the future, I should try to find it and don’t just stick with building a recursive function.

Please check me out on the following social networks as well, I would love to hear from you! — LinkedIn, GitHub, and Facebook.

More content at plainenglish.io

How to Solve the LeetCode Algorithm Challenge: Search Insert Position was originally published in JavaScript in Plain English on Medium, where people are continuing the conversation by highlighting and responding to this story.