# Search Insert Position Python – Leetcode Solutions

LeetCode has an Easy coding Problem in Its’ Algorithm Section “ Search Insert Position Python”. Today We are going to solve this problem. LeetCode Link of the Problem is HERE

#### 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.

You must write an algorithm with `O(log n)` runtime complexity.

Examples

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

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

Input: nums = [1,3,5,6], target = 7
Output: 4
﻿

Constraints:

• `1 <= nums.length <= 104`
• `-104 <= nums[i] <= 104`
• `nums` contains distinct values sorted in ascending order.
• `-104 <= target <= 104`

#### Solution in Python

```class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if target in nums:
return nums.index(target)
else:
for i in range(len(nums)):
if nums[i] > target:
return i

return len(nums)
```

Success
Runtime: 79 ms, faster than 8.75% of Python online submissions for the problem
Memory Usage: 14.3 MB, less than 7.52% of Python online submissions for the problem

Data Structures related posts visit HERE

Python-related posts Visit HERE

C/C++ related posts Visit HERE

Databases related posts Visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Share the Knowledge