-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryGap.cpp
75 lines (56 loc) · 1.38 KB
/
BinaryGap.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
// 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;
// Example program
#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
int solution(int N) {
// write your code in C++14 (g++ 6.2.0)
if (N == 0)
return 0;
//std::cout << " N = " << N << std::endl;
/* if (n > 2147483647)
{
std::cout << "n is not in the range 1.....2147483647 - > " << n << std::endl;
return 0;
}*/
int temp = N;
int count = 0;
while( temp > 0 )
{
if( (temp & 1) == 1)
{
int tmpCount = 0;
while (temp > 0)
{
temp = temp >> 1;
if( (temp & 1) == 0)
{
//count++;
tmpCount++;
//std::cout << "Tmp Count : " << tmpCount << std::endl;
}
else
{
count = std::max(count, tmpCount);
break;
}
}
//count++;
}
else
{
temp = temp >> 1;
}
}
return count;
}
int main()
{
int n = 34;
std::cout << "Longest No of Consecutive 0s " << solution(n) << "!\n";
std::cout << "INT MAX : " << INT_MAX << std::endl;
}