Created
July 12, 2017 08:54
-
-
Save MichaelaIvanova/dbf411d8aeaef48183fa6e6ae33e1fb1 to your computer and use it in GitHub Desktop.
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace algos | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int input = 1041; | |
solution(input); | |
} | |
public static int solution(int n) | |
{ | |
var result = Convert.ToString(n, 2); | |
var gapLenghts = new List<int>(); | |
for (int i = 1; i <= result.Length - 1; i++) | |
{ | |
var previousIndex = i - 1; | |
var nextIndex = i + 1; | |
if (result[i] == '0' && result[previousIndex] == '1' && nextIndex != result.Length - 1) | |
{ | |
var currLent = 0; | |
for (int k = i; k < result.Length; k++) | |
{ | |
if (result[k] != '1') | |
{ | |
currLent++; | |
} | |
else | |
{ | |
gapLenghts.Add(currLent); | |
currLent = 0; | |
break; | |
} | |
} | |
} | |
} | |
var c = 0; | |
if (gapLenghts.Any()) | |
{ | |
var max = gapLenghts.Max(); | |
if (max != 0) | |
{ | |
c = max; | |
} | |
} | |
return c; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment