Convert decimal to binary and find the maximum number of consecutive 1's in the binary Task: given a base- 10 integer, n, convert it to binary (base-2). Then find and print the base- 10 integer denoting the maximum number of consecutive 1's in n's binary representation. 

What can you say about my implementation of the problem?

n = gets.strip.to_i
array = []
while n &gt; 0
  array.push(n % 2)    
  n = n / 2 
end

counter = 0
consec = []
len = array.length

array.each_with_index do |x, index|
  if x == 1
    counter += 1
    consec.push(counter) if index + 1 == len
  else
    consec.push(counter) if counter &gt; 0
    counter = 0
  end
end

puts consec.sort[-1]
You solution looks good for a procedural one -- something similar would be the correct way to do this in, say, C.

That said, and assuming this isn't considered "cheating" for the problem at hand, if you're using ruby, you might as well take advantage of ruby's facilities.  Assuming the input number is n, all you need to do is:

n.to_s(2).scan(/1+/).max_by(&amp;:size).size


or, even more tersely, taking advantage of how strings are compared (ie, "11" &gt; "1" and "111" &gt; "11", etc):

n.to_s(2).scan(/1+/).max.size


to_s(2) converts to the binary string, scan finds all runs of 1s, max_by finds the longest of those, and the final size tells you how long that is, as a decimal.