-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_gap.cpp
More file actions
42 lines (35 loc) · 934 Bytes
/
binary_gap.cpp
File metadata and controls
42 lines (35 loc) · 934 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
38
39
40
41
42
#include <iostream>
int solution(int input){
bool found_1 = false;
int maxbinary_gap = 0;
int count = 0;
while(input){
if(input & 1){
if(found_1 == false){
found_1 = true;
}else{
if(count > maxbinary_gap){
maxbinary_gap = count;
count = 0;
}
found_1 = false;
count = 0;
}
}else{
if(found_1)
count++;
}
input = input >> 1;
}
return maxbinary_gap;
}
int main(){
std::cout << solution(9) << std::endl;
std::cout << solution(15) << std::endl;
std::cout << solution(8) << std::endl;
std::cout << solution(20) << std::endl;
std::cout << solution(529) << std::endl;
std::cout << solution(32) << std::endl;
std::cout << solution(561892) << std::endl;
return 0;
}