-
Notifications
You must be signed in to change notification settings - Fork 0
/
Missing number
53 lines (52 loc) · 958 Bytes
/
Missing number
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
#include <bits/stdc++.h>
int decimal(string a)
{
int k = 0 ;
int dec = 0 ;
for(int i = a.size() - 1 ; i >= 0 ; i--)
{
int d = a[i] - '0' ;
dec = dec + d * pow(2 , k) ;
k++ ;
}
return dec ;
}
string binary(int a)
{
string res = "" ;
while(a != 0)
{
int k = a % 2 ;
if(k == 0)
{
res = '0' + res ;
}
else{
res = '1' + res;
}
a = a/ 2 ;
}
if(res == "")
{
return "0";
}
return res ;
}
string findMissingNumber(vector<string> &binaryNums, int n)
{
// Write your code here.
vector <int> hash(n +1 , 0) ;
for(int i = 0 ; i < binaryNums.size() ;i++)
{
int dec = decimal(binaryNums[i]);
hash[dec]++ ;
}
for(int i = 0 ; i < hash.size() ;i++)
{
if(hash[i] == 0)
{
return binary(i) ;
}
}
return "a" ;
}