Suppose we have a list of numbers from 0 to n. There is one number that is missing. We have to find the missing number in an efficient approach. So if A = [0, 1, 2, 3, 4, 5, 7, 8, 9], missing number is 6.
To solve this, we will use the binary search approach.
Let us see the following implementation to get a better understanding −
class Solution(object): def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ nums.sort() high = len(nums) low = 0 while low<high: mid = low + (high-low)//2 if nums[mid]>mid: high = mid else: low = mid+1 return low ob1 = Solution() print(ob1.missingNumber([5,3,1,7,8,0,9,2,4]))
nums = [5,3,1,7,8,0,9,2,4]