DEV Community

Discussion on: Daily Challenge #215 - Difference of 2

Collapse
 
anjosebar profile image
Antonio J.

Ruby solution

def pair_difference(lst)
  lst.sort # sort array
    .combination(2) # get all posible combinations of pairs
    .select{|arr| (arr.max - arr.min) == 2 } # only combinations that have difference of 2
    .sort_by{|arr| arr[0] } # sort by the first number
end