forked from swaaz/basicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
74 lines (54 loc) · 1.45 KB
/
Copy pathprogram.cpp
File metadata and controls
74 lines (54 loc) · 1.45 KB
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
#include <iostream>
#include <vector>
using namespace std;
class Missing{
public:
int findMissing(vector<int> &ar)
{
int l = 0, r = ar.size();
while (l <= r) {
int mid = (l + r) / 2;
// If this is the first element
// which is not index + 1, then
// missing element is mid+1
if (ar[mid] != mid + 1 &&
ar[mid - 1] == mid)
return mid + 1;
// if this is not the first missing
// element search in left side
if (ar[mid] != mid + 1)
r = mid - 1;
// if it follows index+1 property then
// search in right side
else
l = mid + 1;
}
// if no element is missing
return -1;
}
//O(n) O(1)
int findMissing_II(vector<int> &ar){
int sum = 0;
int n = ar.size();
int total = n+1;
for (int i= 0;i<n;i++){
sum = ar[i]^sum; /* XOR of all the elements of the array */
total = total^(i+1); /* XOR of all the numbers from 1 to n+1 */
}
/* XOR of sum and total gives the missing number.*/
return sum^total;
}
};
int main()
{
vector<int> v;
int n;cin>>n;
for(int i=0;i<n;i++){
int x;cin>>x;
v.push_back(x);
}
Missing m;
cout<< m.findMissing(v)<<endl;
cout<<m.findMissing_II(v);
return 0;
}