-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathSmallestmissingnumber.cpp
More file actions
41 lines (31 loc) · 941 Bytes
/
Smallestmissingnumber.cpp
File metadata and controls
41 lines (31 loc) · 941 Bytes
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
/*
Given an array of sorted integers where each one is in the range from 0 to m-1 and m > n,
where n is the length of this array.
Find the smallest number that is missing from the array.
*/
/*
solution: modified binary search.
O(logn) time, O(1) space
*/
#include<iostream>
using namespace std;
int FindFirstMissing(int arr[], int begin, int end) {
//end+1 is the number larger than the last element of array
if (begin > end)
return end + 1;
if (begin != arr[begin]) return begin;
int mid = (begin + end) / 2;
if (arr[mid] > mid) {
return FindFirstMissing(arr, begin, mid);
} else {
return FindFirstMissing(arr, mid + 1, end);
}
}
int main() {
int arr[]= {0, 1, 2, 6, 9};
int arr1[]= {0, 1, 2, 3, 4};
int len = sizeof(arr)/sizeof(arr[0]);
cout<<FindFirstMissing(arr, 0, len-1)<<endl; //3
cout<<FindFirstMissing(arr1, 0, len-1)<<endl; //5
return 0;
}