C++ Program to Find the smallest missing number
Last Updated :
10 Aug, 2022
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.
C++
#include<bits/stdc++.h>
using namespace std;
int findFirstMissing( int array[],
int start, int end)
{
if (start > end)
return end + 1;
if (start != array[start])
return start;
int mid = (start + end) / 2;
if (array[mid] == mid)
return findFirstMissing(array,
mid+1, end);
return findFirstMissing(array, start, mid);
}
int main()
{
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 10};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "Smallest missing element is " <<
findFirstMissing(arr, 0, n-1) << endl;
}
|
Output
Smallest missing element is 8
Note: This method doesn’t work if there are duplicate elements in the array.
Time Complexity: O(logn)
Auxiliary Space: O(logn)
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:
C++
#include <bits/stdc++.h>
using namespace std;
int findFirstMissing(vector< int > arr , int start ,
int end, int first)
{
if (start < end)
{
int mid = (start + end) / 2;
if (arr[mid] != mid+first)
return findFirstMissing(arr, start,
mid , first);
else
return findFirstMissing(arr, mid + 1,
end , first);
}
return start + first;
}
int findSmallestMissinginSortedArray(vector< int > arr)
{
if (arr[0] != 0)
return 0;
if (arr[arr.size() - 1] == arr.size() - 1)
return arr.size();
int first = arr[0];
return findFirstMissing(arr, 0, arr.size() - 1, first);
}
int main()
{
vector< int > arr = {0, 1, 2, 3, 4, 5, 7};
int n = arr.size();
cout<< "First Missing element is : " <<findSmallestMissinginSortedArray(arr);
}
|
Output
First Missing element is : 6
Time Complexity: O(Logn)
Auxiliary Space: O(logn) where logn is the size of the recursive stack tree
Please refer complete article on Find the smallest missing number for more details!
Similar Reads
How to Find the Smallest Number in an Array in C++?
In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma
2 min read
C++ Program To Find LCM of Two Numbers
LCM (Least Common Multiple) of two numbers is the smallest number that is divisible by both numbers. For example, the LCM of 15 and 20 is 60, and the LCM of 15 and 25 is 75. In this article, we will learn to write a C++ program to find the LCM of two numbers. We can find the LCM of two numbers in C+
4 min read
How to Find the Third Smallest Number in an Array in C++?
In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the third smallest element in an array in C++. If there is no third smallest elemen
2 min read
Find the missing number in unordered Arithmetic Progression
Given an unsorted array arr[] of N integers that are in Arithmetic Progression, the task is to print the missing element from the given series. Examples: Input: arr[] = {12, 3, 6, 15, 18} Output: 9 Explanation: The elements given in order are: 3, 6, 12, 15, 18. Hence missing element is 9. Input: arr
13 min read
C++ Program To Find Sum of First N Natural Numbers
Natural numbers are those positive whole numbers starting from 1 (1, 2, 3, 4, ...). In this article, we will learn to write a C++ program to calculate the sum of the first N natural numbers. AlgorithmInitialize a variable sum = 0.Run a loop from i = 1 to n.Inside the loop, add the value of i to the
1 min read
C++ Program for Smallest K digit number divisible by X
Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples: Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10 An efficient solution would be : Compute MIN : smallest K-digit num
2 min read
C++ Program to Swap Two Numbers
Swapping numbers is the process of interchanging their values. In this article, we will learn algorithms and code to swap two numbers in the C++ programming language. 1. Swap Numbers Using a Temporary VariableWe can swap the values of the given two numbers by using another variable to temporarily st
4 min read
C++ 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
C Program To Find Prime Numbers Between Given Range
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers. In this article, we will learn how to find all the prime numbers between the given range. Example Input: l = 10, r = 30Output: 11 13 17 19Explan
6 min read
C++ Program to find whether a no is power of two
Given a positive integer, write a function to find if it is a power of two or not.Examples : Input : n = 4 Output : Yes 22 = 4 Input : n = 7 Output : No Input : n = 32 Output : Yes 25 = 32 1. A simple method for this is to simply take the log of the number on base 2 and if you get an integer then nu
2 min read