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.
As @clint_carrion says, I think the point here is to know how to go from decimal to binary and to be efficient traversing the string. An easy solution in C# could be the one...:
class Solution {
static void Main(string[] args) {
int n = Convert.ToInt32(Console.ReadLine());
var bn = Convert.ToString(n, 2).Split('0').Select(s => s.Length).Max();
Console.WriteLine(bn);
}
}
... but again, this goes through the string/list at least 4 times: (1) to convert from decimal to binary (2) to split it, (3) to get lengths for each string and (4) to get the max while an efficient solution uses only division, module and a loop (like the one from ashukumar)
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 →
As @clint_carrion says, I think the point here is to know how to go from decimal to binary and to be efficient traversing the string. An easy solution in C# could be the one...:
... but again, this goes through the string/list at least 4 times: (1) to convert from decimal to binary (2) to split it, (3) to get lengths for each string and (4) to get the max while an efficient solution uses only division, module and a loop (like the one from ashukumar)