-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-gap.cpp
More file actions
39 lines (31 loc) · 742 Bytes
/
binary-gap.cpp
File metadata and controls
39 lines (31 loc) · 742 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
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <bitset>
using namespace std;
int solution(int N) {
// write your code in C++14 (g++ 6.2.0)
std::bitset<32> b;
b = std::bitset<32>(N);
//std::cout << std::bitset<32>(N);
//ones = b.count();
int count = -1;
int greatest_count = 0;
for(int x = 32;x >= 0;x-- )
{
if(b[x] == 1)
{
if(count > greatest_count)
{
greatest_count = count;
}
count = 0;
}
else if(count >= 0)
{
count++;
}
}
return greatest_count;
}