Examples :
1. Input: {4, 5, 10, 11}, n = 4, m = 12
Output: 0
2. Input: {0, 1, 2, 3}, n = 4, m = 5
Output: 4
3. Input:
{0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11
Output: 8
#include <bits/stdc++.h>
using namespace std;
int findFirstMissing(int array[],int l, int r)
{
if (l > r)
return r + 1;
if (l != array[l])
return l;
int mid = (l + r) / 2;
// Left half has all elements
// from 0 to mid
if (array[mid] == mid)
return findFirstMissing(array,mid + 1, r);
return findFirstMissing(array, l, mid);
}
// Driver code
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) << rl;
}
Output:
Smallest missing element is 8
0 Comments