Closest Numbers

Solutions Sorting the sequence is the key to solve this challenge. Here is a C++ Solution based on zip | map | reduce pattern: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <iterator>#include <cstdio>#include <vector>#include <iostream>#include <algorithm>#include <numeric>using namespace std; int main() { int N; cin >> N; vector<int> v; v.reserve(N); copy_n(istream_iterator<int>(cin), N, back_inserter(v)); sort(begin(v), end(v)); auto minDiff = inner_product(next(begin(v)), end(v), begin(v), numeric_limits<int>::max(), [](int curr, int nxt){ return min(curr, nxt); }, minus<>{} ); equal(begin(v), end(v)-1, begin(v)+1, [=](int l, int r){ if (abs(r-l) == minDiff) cout << l << "" << r << ""; return true; }); } Here is a Javascript solution by Simone Busoli: [Read More]