Created
March 10, 2015 13:16
-
-
Save aalvarado/b40072166449757abd8a to your computer and use it in GitHub Desktop.
missing int
This file contains 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
# Missing int from unsorted array and with arbitrary starting int | |
def solution(a=[]) | |
sum = 0 | |
min = a.first | |
max = a.last | |
a.each do |e| | |
sum += e | |
min = e if e < min | |
max = e if e > max | |
end | |
min_sum = min > 0 ? ((min) * ( min - 1 )) / 2 : 0 | |
a_sum = (((max + 1) * ( max )) / 2) - min_sum | |
a_sum - sum | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment