-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestBinaryGap.cs
68 lines (58 loc) · 1.96 KB
/
LargestBinaryGap.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
namespace Algorithms
{
public class Program
{
public static void Main(string[] args)
{
List<int> bitsArray = new List<int> {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,1,0,0,1,1,1,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0};
int gap = FindLargestBinaryGap(bitsArray);
Console.WriteLine(gap);
gap = FindLargestBinaryGap(3390383893760);
Console.WriteLine(gap);
}
//Find Binary Gap in a list of binary values
private static int FindLargestBinaryGap(List<int> bits)
{
int count = 0;
int largestGap = 0;
int foundOne = 0;
foreach(int bit in bits)
{
if(bit == 0 && foundOne == 1)
{
count++;
}
else if(bit == 1)
{
foundOne = 1;
largestGap = largestGap > count ? largestGap : count;
count = 0;
}
}
return largestGap;
}
//Overloaded Method to find the Binary Gap of a decimal number
//First Converts the decimal to Binary List
private static int FindLargestBinaryGap(long number)
{
List<int> bitsArray = ConvertToBinary(number);
return FindLargestBinaryGap(bitsArray);
}
//Converts the decimal number to a List of Binary Values
private static List<int> ConvertToBinary(long number)
{
List<int> bits = new List<int>();
int index = 0;
while(number > 0)
{
bits.Add(Convert.ToInt32(number % 2));
index++;
number = number / 2;
}
bits.Reverse();
return bits;
}
}
}