-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodility_L1(Iterations)_BinaryGap.cpp
More file actions
63 lines (53 loc) · 1.19 KB
/
Codility_L1(Iterations)_BinaryGap.cpp
File metadata and controls
63 lines (53 loc) · 1.19 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
#include <vector>
#include <algorithm>
// Binary representation of num. (num >= 0)
std::vector<bool> to_binary(int num) noexcept
{
std::vector<bool> binary;
while (num)
{
binary.push_back(num % 2);
num >>= 1;
}
return binary;
}
// Initial solution.
int solution(int N) noexcept
{
const auto binary = to_binary(N);
size_t maxGap = 0;
auto iter = binary.cbegin();
const auto iterEnd = binary.cend();
while (iter != iterEnd)
{
auto currentTrue = std::find(iter, iterEnd, true);
if (currentTrue == iterEnd) break;
auto nextTrue = std::find(currentTrue + 1, iterEnd, true);
if (nextTrue == iterEnd) break;
const auto gap = std::distance(currentTrue, nextTrue) - 1;
maxGap = std::max(maxGap, static_cast<size_t>(gap));
iter = nextTrue;
}
return maxGap;
}
// Simple solution without helper function, vectors and iterators.
int simpleSolution(int N) noexcept
{
int maxGap = 0;
int prev = -1;
int current = 0;
while (N)
{
bool bit = N % 2;
N >>= 1;
if (bit)
{
int gap;
prev == -1 ? gap = 0 : gap = current - prev - 1;
prev = current;
maxGap = std::max(maxGap, gap);
}
++current;
}
return maxGap;
}