Sorry for the delay between uploads. Here is the next problem I will tackle:
Here are some examples of output:
For this problem, you do not need to overthink the solution. It is quite easy. There are many possible ways to go about this problem. I decided to take the simple and clean route with my solution. Here was my plan:
- Traverse the array
- Check if the value of the array at each index was equal to the target
- If so, return the index value
- If not, then check if the value of the array at each index was less than the target
- If so, return the index value
- If you traverse through the whole array and still don’t return anything, then the target value is greater than the largest number in the array, and therefore, you can return the length of the array(which would return the index of a number that would be appended to the array)
Here is how I went about doing that:
While I know that a binary search would also be a very efficient way to solve this, memory usage wise, it was still very efficient runtime wise.
Here are the logistics of my solution:
Quite a good result, if I do say so myself. If you have any suggestions, be sure to let me know in the comments section below. I hope you guys at least learned something today, and that you all have a great day!