-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (51 loc) · 1.5 KB
/
main.cpp
File metadata and controls
61 lines (51 loc) · 1.5 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
#include <iostream>
#include <vector>
class BitInteger {
private:
std::vector<bool> storage;
public:
static const int max = 64;
int fetch(int index) {
return int {storage[BitInteger::max - 1 - index]};
}
BitInteger(int num) {
std::vector<bool> tempStorage(BitInteger::max);
int counter = 0;
while (num > 0) {
tempStorage[BitInteger::max - 1 - counter] = num % 2;
counter++;
num >>= 1;
}
storage = tempStorage;
}
};
int missingHelper(std::vector<BitInteger>& input, int index);
int findMissing(std::vector<BitInteger>& nums) {
return missingHelper(nums, 0);
}
int missingHelper(std::vector<BitInteger>& input, int index) {
if (index >= BitInteger::max) {
return 0;
}
std::vector<BitInteger> oneBits;
std::vector<BitInteger> zeroBits;
for (auto& i: input) {
if (i.fetch(index) == 0) {
zeroBits.push_back(i);
} else {
oneBits.push_back(i);
}
}
if (zeroBits.size() <= oneBits.size()) {
int v = missingHelper(zeroBits, index + 1);
return (v << 1) | 0;
} else {
int v = missingHelper(oneBits, index + 1);
return (v << 1) | 1;
}
}
int main() {
std::vector<BitInteger> nums {BitInteger(0), BitInteger(1), BitInteger(2), BitInteger(3), BitInteger(5), BitInteger(6), BitInteger(7), BitInteger(8), BitInteger(9)};
std::cout << findMissing(nums) << std::endl;
return 0;
}