We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine().Trim());
string binary = Convert.ToString(n ,2);
char[] c = binary.ToCharArray();
int iCount = 1;
int left = 0;
int right = 1;
// This list will be used to store each count.
// After each iteration, we add the count to the list.
// After the loop has finished, we sort the elements in the
//list,
// and finally, we print the last element of the list.
List<int> lstCounts = new List<int>();
for(int i = 0 ; i < c.Length ; i++)
{
if(right <= c.Length -1 )
{
if(c[left] == c[right] && c[right] == '1')
{
iCount++;
lstCounts.Add(iCount);
}
else
{
lstCounts.Add(iCount);
iCount = 1;
}
}
left++;
right++;
}
// sort elements from the list
lstCounts.Sort();
//print the highest count from the list
Console.Write(lstCounts.LastOrDefault());
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →
My C# solution
public static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine().Trim());