-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26_missingNo.cpp
46 lines (41 loc) · 950 Bytes
/
26_missingNo.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
//LC 268. Missing Number
//Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
//TC = O(n) and SC = O(1)
#include <bits/stdc++.h>
using namespace std;
int missingNo(vector<int> &arr) {
int hash[arr.size()+1] = {0};
for(int i=0; i<arr.size(); i++) {
hash[arr[i]]++;
}
for(int i=0; i<=arr.size(); i++){
if(hash[i] == 0){
return i;
}
}
return -1;
}
int main() {
int n;
cin>>n;
vector<int> arr;
for(int i =0; i<n; i++) {
int a ;
cin >>a;
arr.push_back(a);
}
cout<<missingNo(arr);
return 0;
}
/*for(int i = 0; i<=nums.size(); i++) {
int flag = 0;
for(int j = 0; j<nums.size(); j++){
if (i == nums[j]){
flag = 1;
break;
}
}
if(flag == 0) return i;
}
return -1;
*/