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

# 35. Search Insert Position

```java
class Solution {
    public int searchInsert(int[] nums, int target) {
        int start = 0;
        int end  = nums.length;
        while (start < end){
            int mid = (start+end)/2;
            if (nums[mid]>=target && (mid == 0 || nums[mid-1]<target )) return mid;
            if (target>nums[mid]){
                start = mid+1;
            }else{
                end = mid;
            }
        }
        return nums.length;
    }
}
```

```java
class Solution {
    public int searchInsert(int[] nums, int target) {
        int start = 0;
        int end  = nums.length;
        while (start < end){
            int mid = (start+end)/2;
            if (target <= nums[mid]){
                end = mid;     
            }else{
                start = mid+1;
            }
        }
        return start;
    }
}
```
