-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissing_number_iterative.cpp
83 lines (54 loc) · 1.6 KB
/
missing_number_iterative.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<vector>
using namespace std;
int missing(vector<int> v, int left, int right )
{
// intialisation of algorithm
// v[0] creates the offset of indices in ordered array with no duplication
int m, number;
// the following if statement is included under the condition that the first element in the sorted list should always be 0. This takes into account the extreme case that the first element is missing. This is for leetcode.
if(v[0] == 0)
{
while(left <= right)
{
m = left + (right - left)/2;
if(v[m] == (v[0] + m)) left = m + 1; // everything to the left to and up to index m is ordered; search to the right.
else if( v[m] > (v[0] + m) ) right = m - 1; // since index is not lined up it must be that the missing number is to the left.
}
number = left; // using left since once this is equal to right (the last value of left or right is equal according to the termination condition of while loop)
//extreme cases: last element is missing
if(v[v.size() - 1] == (v[0] + m))
{
number = v[v.size() -1] + 1;
}
}
else // it must be that zero is missing
{
number = 0;
}
// return (v[0] + m); assumption that the sorted list starts at a number not equal to zero
return number;
}
int main()
{
int size = 2;
vector<int> v(size);
v[0] = 1;
v[1] = 0;
/*
v[0] = 0;
v[1] = 1;
v[2] = 2;
v[3] = 3;
v[4] = 4;
v[5] = 5;
v[6] = 6;
v[7] = 7;
v[8] = 8;
v[9] = 9;
*/
int left = 0;
int right = v.size() - 1;
cout << missing(v, left, right) << endl;
return 0;
}