---
# System prepended metadata

title: 'Search Insert Position <img src="https://img.shields.io/badge/-easy-brightgreen">'
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Search Insert Position <img src="https://img.shields.io/badge/-easy-brightgreen">

Given a sorted array 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.

You may assume no duplicates in the array.

<ins>**Example 1:**</ins>

>Input: [1,3,5,6], 5
>Output: 2

<ins>**Example 2:**</ins>

>Input: [1,3,5,6], 2
>Output: 1

<ins>**Example 3:**</ins>

>Input: [1,3,5,6], 7
>Output: 4

<ins>**Example 4:**</ins>

>Input: [1,3,5,6], 0
>Output: 0

```python=
class Solution:
    def __searchInsert(self, nums, target, l, r):
        mid = l + (r - l) // 2

        if l == r:
            return l if target <= nums[l] else l+1
     
        if target < nums[mid]:
            r = mid
        elif target > nums[mid]:
            l = mid + 1
        else:
            return mid
        
        return self.__searchInsert(nums, target, l, r)
        
    
    def searchInsert(self, nums: List[int], target: int) -> int:
        n = len(nums)
        if n == 0:
            return 0
        else:
            return self.__searchInsert(nums, target, 0, n-1)
```