Search insert position in python using binary search

 class Solution:

    def searchInsert(self, nums: List[int], target: int) -> int:

        res=0

        l=0

        r=len(nums)

        while(l<r):

            m=(l+r)//2

            if nums[m]==target:

                res=m

                break

            elif target>nums[l]:

                l+=1

                res=l

            else:

                r-=1

        return res

Comments

Popular posts from this blog

Create account in java |deposit money|withdraw money

Library Management System in python OOPs Concept

List comprehension hackerrank solution in python