Python3 Program to Find the smallest missing number
Last Updated :
06 Sep, 2024
Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array.
Examples
Input: {0, 1, 2, 6, 9}, n = 5, m = 10
Output: 3
Input: {4, 5, 10, 11}, n = 4, m = 12
Output: 0
Input: {0, 1, 2, 3}, n = 4, m = 5
Output: 4
Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11
Output: 8
Thanks to Ravichandra for suggesting following two methods.
Method 1 (Use Binary Search)
For i = 0 to m-1, do binary search for i in the array. If i is not present in the array then return i.
Time Complexity: O(m log n)
Method 2 (Linear Search)
If arr[0] is not 0, return 0. Otherwise traverse the input array starting from index 0, and for each pair of elements a[i] and a[i+1], find the difference between them. if the difference is greater than 1 then a[i]+1 is the missing number.
Time Complexity: O(n)
Method 3 (Use Modified Binary Search)
Thanks to yasein and Jams for suggesting this method.
In the standard Binary Search process, the element to be searched is compared with the middle element and on the basis of comparison result, we decide whether to search is over or to go to left half or right half.
In this method, we modify the standard Binary Search algorithm to compare the middle element with its index and make decision on the basis of this comparison.
- If the first element is not same as its index then return first index
- Else get the middle index say mid
- If arr[mid] greater than mid then the required element lies in left half.
- Else the required element lies in right half.
Python3
# Python3 program to find the smallest
# elements missing in a sorted array.
def findFirstMissing(array, start, end):
if (start > end):
return end + 1
if (start != array[start]):
return start;
mid = int((start + end) / 2)
# Left half has all elements
# from 0 to mid
if (array[mid] == mid):
return findFirstMissing(array,
mid+1, end)
return findFirstMissing(array,
start, mid)
# driver program to test above function
arr = [0, 1, 2, 3, 4, 5, 6, 7, 10]
n = len(arr)
print("Smallest missing element is",
findFirstMissing(arr, 0, n-1))
# This code is contributed by Smitha Dinesh Semwal
OutputSmallest missing element is 8
Note: This method doesn't work if there are duplicate elements in the array.
Time Complexity: O(Logn)
Space Complexity: O(1)
The space complexity of this algorithm is O(1) because it is using constant space to search the smallest missing element.
Another Method: The idea is to use Recursive Binary Search to find the smallest missing number. Below is the illustration with the help of steps:
- If the first element of the array is not 0, then the smallest missing number is 0.
- If the last elements of the array is N-1, then the smallest missing number is N.
- Otherwise, find the middle element from the first and last index and check if the middle element is equal to the desired element. i.e. first + middle_index.
- If the middle element is the desired element, then the smallest missing element is in the right search space of the middle.
- Otherwise, the smallest missing number is in the left search space of the middle.
Below is the implementation of the above approach:
Python3
# Python3 program for above approach
# Function to find Smallest
# Missing in Sorted Array
def findSmallestMissinginSortedArray(arr):
# Check if 0 is missing
# in the array
if (arr[0] != 0):
return 0
# Check is all numbers 0 to n - 1
# are present in array
if (arr[-1] == len(arr) - 1):
return len(arr)
first = arr[0]
return findFirstMissing(arr, 0,
len(arr) - 1, first)
# Function to find missing element
def findFirstMissing(arr, start, end, first):
if (start < end):
mid = int((start + end) / 2)
# Index matches with value
# at that index, means missing
# element cannot be upto that point
if (arr[mid] != mid + first):
return findFirstMissing(arr, start,
mid, first)
else:
return findFirstMissing(arr, mid + 1,
end, first)
return start + first
# Driver code
arr = [ 0, 1, 2, 3, 4, 5, 7 ]
n = len(arr)
# Function Call
print("First Missing element is :",
findSmallestMissinginSortedArray(arr))
# This code is contributed by rag2127
OutputFirst Missing element is : 6
Time Complexity: O(Log n)
Auxiliary Space: O(log n) where log(n) is the size of the recursive call stack
Please refer complete article on Find the smallest missing number for more details!
Similar Reads
Python program to find the smallest number in a file Given a text file, write a Python program to find the smallest number in the given text file.Examples:Input: gfg.txtOutput: 9Explanation: Contents of gfg.txt: I live at 624 Hyderabad.My mobile number is 52367. My favourite number is 9.Numbers present in the text file are 9,624,52367Minimum number is
3 min read
Python program to find smallest number in a list In this article, we will discuss various methods to find smallest number in a list. The simplest way to find the smallest number in a list is by using Python's built-in min() function.Using min()The min() function takes an iterable (like a list, typle etc.) and returns the smallest value.Pythona = [
2 min read
Python Program to Find LCM of Two Numbers We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python.Example:Input: a = 12, b = 15Output: 60Explanation: LCM of 12 and 15 is 60Python Program to Find LCM of Two NumbersBelow
3 min read
Python Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9};
4 min read
Python Program to Find the Sum of Natural Numbers Using While Loop Calculating the Sum of N numbers in Python using while loops is very easy. In this article, we will understand how we can calculate the sum of N numbers in Python using while loop.Calculate Sum of Natural Numbers in Python Using While LoopBelow are some of the examples by which we can see how we can
2 min read
Python | Find missing numbers in a sorted list range Given a range of sorted list of integers with some integers missing in between, write a Python program to find all the missing integers. Examples: Input : [1, 2, 4, 6, 7, 9, 10] Output : [3, 5, 8] Input : [5, 6, 10, 11, 13] Output : [7, 8, 9, 12] Method #1: List comprehension Python # Python3 progra
5 min read
Sum the Digits of a Given Number - Python The task of summing the digits of a given number in Python involves extracting each digit and computing their total . For example, given the number 12345, the sum of its digits is 1 + 2 + 3 + 4 + 5 = 15. Using modulo (%)This method efficiently extracts each digit using the modulus (%) and integer di
2 min read
Minimum of two numbers in Python In this article, we will explore various methods to find minimum of two numbers in Python. The simplest way to find minimum of two numbers in Python is by using built-in min() function. Pythona = 7 b = 3 print(min(a, b))Output3 Explanation:min() function compares the two numbers a and b.So, it retur
2 min read
Python Program for k-th missing element in sorted array Given an increasing sequence a[], we need to find the K-th missing contiguous element in the increasing sequence which is not present in the sequence. If no k-th missing element is there output -1. Examples : Input : a[] = {2, 3, 5, 9, 10}; k = 1; Output : 1 Explanation: Missing Element in the incre
4 min read
Find Square Root Of Given Number - Python Given an integer X, find its square root. If X is not a perfect square, then return floor(âx). For example, if X = 11, the output should be 3, as it is the largest integer less than or equal to the square root of 11.Using built-in functionsWe can also find the floor of the square root using Pythonâs
3 min read