-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryGap.cpp
More file actions
44 lines (38 loc) · 732 Bytes
/
BinaryGap.cpp
File metadata and controls
44 lines (38 loc) · 732 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
//
// Note: Some test cases failed, need to improve this solution
//
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <bitset>
using namespace std;
int solution(int);
int main() {
int num; cin >> num;
cout << solution(num)<<endl;
return 0;
}
int solution(int N) {
string binary = bitset<32>(N).to_string();
cout << binary << endl;
int countZero = 0;
int nTemp = 0;
for (int i = 0; i < binary.length(); i++){
if (binary[i] == '1')
{
int j = i;
nTemp = countZero;
countZero = 0;
while (binary[j + 1] != '1' && binary[j + 1] != NULL) {
countZero++;
j++;
}
if (nTemp > countZero) {
countZero = nTemp;
return countZero;
}
}
}
return 0;
}