-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinaryGap.cs
37 lines (33 loc) · 907 Bytes
/
BinaryGap.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
33
34
35
36
37
using System;
namespace Test
{
public class BinaryGap
{
static void Main(string[] args)
{
Console.WriteLine(Solution(2147483647));
Console.WriteLine(Solution(529));
Console.WriteLine(Solution(1041));
Console.WriteLine(Solution(1));
Console.WriteLine(Solution(3));
Console.WriteLine(Solution(5));
Console.WriteLine(Solution(32));
}
static int Solution(int N)
{
int Longest = 0, ZeroCtr = 0;
foreach (char c in Convert.ToString(N, 2))
{
if (c == '0')
ZeroCtr++;
else
{
if (ZeroCtr > Longest)
Longest = ZeroCtr;
ZeroCtr = 0;
}
}
return Longest;
}
}
}