Created
March 10, 2013 02:50
-
-
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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