Skip to content

Instantly share code, notes, and snippets.

@gogogarrett
Created March 10, 2013 02:50
Show Gist options
  • Select an option

  • Save gogogarrett/5126890 to your computer and use it in GitHub Desktop.

Select an option

Save gogogarrett/5126890 to your computer and use it in GitHub Desktop.
Given an array [11, 3, 7, 1], this will compute how many steps it takes in order to make every element the same value. [6,6,6,6]
def equalization_steps(a)
array, steps = a, 0
begin
a.each_with_index do |v, i|
if v > a[ (i + 1) % a.length ]
array[i] -= 1
else
array[i] += 1
end
end
steps += 1
end while array.uniq.size != 1
steps
end
equalization_steps([11, 3, 7, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment