-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingNumber.cpp
More file actions
59 lines (45 loc) · 950 Bytes
/
MissingNumber.cpp
File metadata and controls
59 lines (45 loc) · 950 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
void swap(int*a,int*b){
int temp = *a;
*a = *b;
*b = temp;
}
int gcd(int a,int b){
if(a > b)return gcd(b,a);
while(b%a){
a = b%a;
}
return a;
}
void solve(vector<int>array,int n){
sort(array.begin(),array.end());
int l = 0;
int r = n-1;
if(array[0] != 1){cout << 1;return;}
if(array[n-2] != n){cout << n ;return;}
while(l < r){
int mid = (l + r + 1)/2;
if(array[mid] - array[mid-1] >= 2){
l = mid+1;
break;
}else if(array[mid+1] - array[mid] >= 2){
l = mid+2;break;
}
if(array[mid] > mid + 1){
r = mid;
}else{
l = mid;
}
}
cout << l;
return;
}
int main(){
int n;
cin >> n;
vector<int>array(n-1);
for(int i = 0 ; i < n-1 ; i ++)cin >> array[i];
solve(array,n);
return 0;
}