-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
smallest_missing.cpp
59 lines (49 loc) · 1.33 KB
/
smallest_missing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Given a sorted array of distinct non-negative integers, find smallest missing element in it.
* For example,
* Input: A[] = [0, 1, 2, 6, 9, 11, 15]
* Output: The smallest missing element is 3
* Input: A[] = [1, 2, 3, 4, 6, 9, 11, 15]
* Output: The smallest missing element is 0
* Input: A[] = [0, 1, 2, 3, 4, 5, 6]
* Output: The smallest missing element is 7
*/
#include <iostream>
#include <vector>
int missingElement(const std::vector<int>& arr, int low, int high)
{
if (low > high)
{
return low;
}
int mid = low + (high - low)/2;
// if mid index matches the with the mid element, then smallest
// missing element lies on the right of the mid.
if (arr[mid] == mid)
{
return missingElement(arr, mid + 1, high);
}
else
{
return missingElement(arr, low, mid - 1);
}
}
void print(const std::vector<int>& arr)
{
std::cout << "Contents of array:" << std::endl;
for (auto i : arr)
{
std::cout << i << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> arr{0, 1, 2, 3, 4, 6, 7};
print(arr);
int low = 0;
int high = arr.size() -1;
std::cout << "Smallest missing element in array is :" << missingElement(arr, low, high)
<< std::endl;
return 0;
}