-
Notifications
You must be signed in to change notification settings - Fork 15
/
BinaryGap-v2.cs
32 lines (30 loc) · 877 Bytes
/
BinaryGap-v2.cs
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
public static int Solution(int N)
{
var sequenceCounter = 0;
var iterations = Math.Floor(Math.Log(N, 2) + 1);
var maxSequence = 0;
var startingOne = false;
for (int i = 0; i < iterations; i++)
{
// check if binary digit is 0
var isCero = (N & (1 << i)) == 0;
Console.Write(isCero ? "0" : "1");
if (isCero && startingOne)
{
// we are in a 0, only count if there was a 1 before
sequenceCounter++;
}
else if (!isCero)
{
// we are in a 1 and the sequence had already started
if (startingOne && sequenceCounter > maxSequence)
{
maxSequence = sequenceCounter;
}
// restart the sequence
startingOne = true;
sequenceCounter = 0;
}
}
return maxSequence;
}