I am trying to build a function that counts the number of zero between ones. My function is doing just fine for binaries that starts and end with one. But the problem is if the given binary is 100000, it is returning 5. But it should return zero because it is not between ones.
Here is the code.
private static int solution1(int N)
{
string binary = Convert.ToString(N, 2);
int gap = 0;
int longestgap = 0;
foreach (char Z in binary)
{
if (Z == '0') gap++;
if (gap > longestgap) longestgap = gap;
if (Z == '1') gap = 0;
}
return longestgap;
}