Given a sorted array arr[], 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: arr[] = [0, 1, 2, 6, 9], m = 10
Output: 3
Explanation: Missing numbers are 3, 4, 5, 7 and 8. But the smallest missing number is 3.Input: arr[] = [4, 5, 10, 11], m = 12
Output: 0
Input: arr[] = [0, 1, 2, 3], m = 5
Output: 4
Table of Content
[Naive Approach] Using Linear Search - O(n) Time and O(1) Space
In a sorted array of distinct non-negative integers, if no number is missing, each element should satisfy arr[i] = i. The array is scanned linearly to find the first index i where arr[i] != i. This index represents the smallest missing number. If no such index exists, it means all numbers from 0 to n-1 are present, so the smallest missing number is n (i.e., arr[n-1] + 1).
#include <iostream>
#include <vector>
using namespace std;
int findFirstMissing(vector<int> &arr) {
int n = arr.size();
for (int i = 0; i < n; i++) {
if (arr[i] != i)
return i;
}
// If all elements match their indices
return n;
}
int main() {
vector<int> arr = {0, 1, 2, 3, 4, 5, 6, 7, 10};
cout << "Smallest missing element is "
<< findFirstMissing(arr) << endl;
return 0;
}
class GFG {
static int findFirstMissing(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
if (arr[i] != i)
return i;
}
// If all elements match their indices
return n;
}
public static void main(String[] args) {
int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 10};
System.out.println("Smallest missing element is " +
findFirstMissing(arr));
}
}
def findFirstMissing(arr):
n = len(arr)
for i in range(n):
if arr[i] != i:
return i
# If all elements match their indices
return n
if __name__ == "__main__":
arr = [0, 1, 2, 3, 4, 5, 6, 7, 10]
print("Smallest missing element is",
findFirstMissing(arr))
using System;
class GFG {
static int findFirstMissing(int[] arr) {
int n = arr.Length;
for (int i = 0; i < n; i++) {
if (arr[i] != i)
return i;
}
// If all elements match their indices
return n;
}
static void Main() {
int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 10};
Console.WriteLine("Smallest missing element is " +
findFirstMissing(arr));
}
}
function findFirstMissing(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
if (arr[i] !== i)
return i;
}
// If all elements match their indices
return n;
}
// Driver code
let arr = [0, 1, 2, 3, 4, 5, 6, 7, 10];
console.log("Smallest missing element is",
findFirstMissing(arr));
Output
Smallest missing element is 8
[Expected Approach] Using Binary Search - O(log(n)) Time and O(1) Space
Since the array is sorted and contains distinct non-negative integers, this property allows us to apply binary search.
If at any index mid:
- arr[mid] == mid, then all numbers from 0 to mid are present, so the smallest missing number must lie in the right half.
- arr[mid] > mid, then a number is missing in the left half, including possibly mid itself.
By repeatedly narrowing the search space using this condition, binary search converges to the first index where arr[i] ≠ i.
After the search ends, the pointer low indicates the smallest missing number.
#include <iostream>
#include <vector>
using namespace std;
int findFirstMissing(vector<int> &arr) {
int low = 0, high = arr.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// If value matches index, missing element is on the right
if (arr[mid] == mid)
low = mid + 1;
// Otherwise, missing element is on the left
else
high = mid - 1;
}
return low;
}
int main() {
vector<int> arr = {0, 1, 2, 3, 4, 5, 6, 7, 10};
cout << "Smallest missing element is "
<< findFirstMissing(arr) << endl;
}
class GFG {
static int findFirstMissing(int[] arr) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// If value matches index, missing element is on the right
if (arr[mid] == mid)
low = mid + 1;
// Otherwise, missing element is on the left
else
high = mid - 1;
}
return low;
}
public static void main(String[] args) {
int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 10};
System.out.println("Smallest missing element is " +
findFirstMissing(arr));
}
}
def findFirstMissing(arr):
low = 0
high = len(arr) - 1
while low <= high:
mid = low + (high - low) // 2
# If value matches index, missing element is on the right
if arr[mid] == mid:
low = mid + 1
# Otherwise, missing element is on the left
else:
high = mid - 1
return low
if __name__ == "__main__":
arr = [0, 1, 2, 3, 4, 5, 6, 7, 10]
print("Smallest missing element is",
findFirstMissing(arr))
using System;
class GFG {
static int findFirstMissing(int[] arr) {
int low = 0;
int high = arr.Length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// If value matches index, missing element is on the right
if (arr[mid] == mid)
low = mid + 1;
// Otherwise, missing element is on the left
else
high = mid - 1;
}
return low;
}
static void Main() {
int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 10};
Console.WriteLine("Smallest missing element is " +
findFirstMissing(arr));
}
}
function findFirstMissing(arr) {
let low = 0;
let high = arr.length - 1;
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
// If value matches index, missing element is on the right
if (arr[mid] === mid)
low = mid + 1;
// Otherwise, missing element is on the left
else
high = mid - 1;
}
return low;
}
// Driver code
let arr = [0, 1, 2, 3, 4, 5, 6, 7, 10];
console.log("Smallest missing element is",
findFirstMissing(arr));
Output
Smallest missing element is 8