svmartin
3/4/2017 - 2:34 AM

missing.rb

# Write a method that takes a sorted array of integers as an argument,
# and returns an array that includes all of the missing integers (in order)
# between the first and last elements of the argument.

def missing(array)
  result = []
  return result if array.size <= 1
  full_array = (array[0]..array[-1]).to_a

  full_array.each do |ele|
    result << ele unless array.include?(ele)
  end

  result
end

p missing([-3, -2, 1, 5])
p missing([-3, -2, 1, 5]) == [-1, 0, 2, 3, 4]
p missing([1, 2, 3, 4]) == []
p missing([1, 5]) == [2, 3, 4]
p missing([6]) == []