> For the complete documentation index, see [llms.txt](https://beesec.gitbook.io/algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://beesec.gitbook.io/algorithms/binary-search/introduction/search-insert-position.md).

# Search Insert Position

{% hint style="info" %}
<https://leetcode.com/problems/search-insert-position/>
{% endhint %}

{% tabs %}
{% tab title="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.

```
Input: nums = [1,3,5,6], target = 5
Output: 2
```

{% endtab %}

{% tab title="Answer" %}

* When do we exit the loop?&#x20;

We exit the loop if left < right

* How to initialise the boundary variable left and right

Left is set to 0. If our target value is larger than every val in the array, we want it to be placed at the end. So right is up to len(right), instead of len(right) - 1

* How to update the boundary? How to choose the appropriate combination from `left = mid, left = mid + 1` and `right = mid, right = mid - 1`?

We just want the normal search here. Right = mid, left = mid + 1.

```python
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        l, r = 0, len(nums)
        while l < r:
            mid = (l + r) // 2
            if nums[mid] >= target:
                r = mid
            else:
                l = mid + 1
        return l
```

Perfectly normal binary search! We go from the left to the right because it is sorted. When we hit `len(nums)` we are at a position that doesn't exist yet, so we return that position to be the point where `target` is supposed to be inserted.

This is because target is greater than the maximum value of the array.
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://beesec.gitbook.io/algorithms/binary-search/introduction/search-insert-position.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
