Skip to content

Instantly share code, notes, and snippets.

@treva-123mutebi
Forked from SourceCode/BinaryGap.cs
Created April 2, 2022 13:39
Show Gist options
  • Select an option

  • Save treva-123mutebi/d19a144bb6d038d4210ec5b2138bcd39 to your computer and use it in GitHub Desktop.

Select an option

Save treva-123mutebi/d19a144bb6d038d4210ec5b2138bcd39 to your computer and use it in GitHub Desktop.
Binary Gap - Codibility - C#
using System;
namespace ObjectApp1
{
class Program
{
static void Main(string[] args)
{
var S = new Solution();
Console.WriteLine(S.solution(0));
}
}
class Solution
{
public int solution(int n)
{
string bits = Convert.ToString(n, 2);
//Console.WriteLine($"Bit String: {bits}");
int longest = 0;
int curCount = 0;
for (int i = 0; i < bits.Length; i++)
{
if (bits[i] == '0')
{
if (curCount > 0) curCount++;
else curCount = 1;
} else curCount = 0;
if (curCount > longest) longest = curCount;
}
return longest;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment