You are viewing a single comment's thread. Return to all comments →
#include <iostream> #include <vector> #include <algorithm> #include <climits> using namespace std; int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr.begin(), arr.end()); int minDiff = INT_MAX; vector<pair<int, int>> closestPairs; for (int i = 0; i < n - 1; i++) { int diff = arr[i + 1] - arr[i]; if (diff < minDiff) { minDiff = diff; closestPairs.clear(); closestPairs.push_back({arr[i], arr[i + 1]}); } else if (diff == minDiff) { closestPairs.push_back({arr[i], arr[i + 1]}); } } for (const auto& pair : closestPairs) { cout << pair.first << " " << pair.second << " "; } cout << endl; return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Closest Numbers
You are viewing a single comment's thread. Return to all comments →