
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Missing Number in Python
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.
- sort the list in ascending order
- high = length of A, and low = 0
- while low < high, do
- mid = low + (high – low)/2
- if A[mid] > mid
- high = mid
- otherwise
- low = mid + 1
- return low
Example
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]))
Input
nums = [5,3,1,7,8,0,9,2,4]
Output
6
- Related Articles
- Program to find kth missing positive number in an array in Python
- First Missing Positive in Python
- Missing Number In Arithmetic Progression using C++
- How to find missing number in A.P.?
- Find missing number in a sequence in C#
- Handling missing keys in Python dictionaries
- Program to find the kth missing number from a list of elements in Python
- Find missing elements in List in Python
- Find the missing number in Arithmetic Progression in C++
- Find the missing number in Geometric Progression in C++
- Find the one missing number in range using C++
- Python – How to check missing dates in Pandas
- Finding one missing number in a scrambled sequence using JavaScript
- Finding the missing number in an arithmetic progression sequence in JavaScript
- Python - Remove the missing (NaN) values in the DataFrame

Advertisements