See the original problem on HackerRank.
Solutions
Sorting the sequence is the key to solve this challenge.
Here is a C++ Solution based on zip | map | reduce pattern:
|
|
Here is a Javascript solution by Simone Busoli:
|
|
An Haskell solution by Alessandro Pezzato
Sort the list, make a list of pairs of adjacent numbers, calculate differences
between them and filter the list, takin only pairs where the difference is equal
to the minimum. We use list comprehension and pattern matching to filter and
translate pairs to lists of two elements, so they can be concatenated by concat
.
|
|
A C solutyion by Michele Liberi
|
|
A Swift solution by Pietro Basso
|
|