# Insert Num Into Right Index

11 May 2020

## Problem Statement

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

## Solution

The first solution I came up with starts by sorting my array of numbers into smallest to biggest using the sort method. Then, if the number we are inserting is the same as one of the numbers in the array, I return the index of the matching number in the array, which is where we'll insert its match. If the number we want to insert is bigger than all of the numbers in our array, then we add it to the end of the array.

If the number we are given is not bigger than any of the numbers in our array, and is not the same as any of the numbers in our array, then we map our original array into a new array, where each value is the difference between the number in the array and the number we want to insert. We then get rid of all of the minus numbers (which means any number that is smaller than our inserted number), and find the smallest difference (the closest number that is bigger than the number we want to insert). Then, we find the index of the smallest difference in the full difference array (with the negative values included), and return it's index, which will be the same as the index we want to insert our number into our original array.

This one was pretty difficult for me to work out.

```
function getIndexToIns(arr, num) {
arr.sort((a,b)=> a-b);
if(arr.indexOf(num) > -1) { return arr.indexOf(num); };
if(Math.max(...arr) > num) { return arr.length; }
const diff = arr.map(e => e - num);
const min_diff = Math.min(...diff.filter(e => e >= 0));
return diff.indexOf(min_diff);
}
getIndexToIns([3,10,5], 3);
```

As always, I asked for feedback for my solution, and was able to shorten the solution using the findIndex function, which returns the index of the number that is greater than or equal to the size of the number we want to insert. If we get back a -1, this means that our number is bigger that the number we want to insert is bigger, so we add it to the end of the array. Otherwise, we return the index we want to insert it into. My solution did the same thing, but in a few more steps because I didn't know that the findIndex function existed. Fun!

```
function getIndexToIns(arr, num) {
arr.sort((a,b)=>a-b);
const index = arr.findIndex(e => e >= num);
if (index === -1) { return arr.length; }
return index;
}
getIndexToIns([3,10,5], 3);
```