Search Insert Position LeetCode June Day 10


Quoestion: ClickHere
Example 1:
Input: [1,2,3,4,5], 2
Output: 1

Example 2:
Input: [34,56,87,95], 26
Output: 0

Example 3:
Input:[200,250,366,465,592], 343
Output:2

Algorithm:
Step1:Check if target is in array or not. If Yes return the index of target
Step2:If target is not in array then add it to array
Step3:sort the array
Step4:Return the index of target

Python Code: 

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        if target in nums:                     #Check if element is in Array or Not
            return nums.index(target)     #return index of target
        else:
            nums.append(target)          #add target to array
            nums.sort()                        #Sort the Array
            return nums.index(target)     #return index of target

#I Hope You Are Satisfied. Do Follow for More Problems






Post a Comment

0 Comments