Skip to content

Instantly share code, notes, and snippets.

@aalvarado
Created March 10, 2015 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalvarado/b40072166449757abd8a to your computer and use it in GitHub Desktop.
Save aalvarado/b40072166449757abd8a to your computer and use it in GitHub Desktop.
missing int
# 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