-
-
Save treva-123mutebi/d19a144bb6d038d4210ec5b2138bcd39 to your computer and use it in GitHub Desktop.
Binary Gap - Codibility - C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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