You are viewing a single comment's thread. Return to all comments →
vector<int> closestNumbers(vector<int> arr) { sort(arr.begin(), arr.end()); vector<int> pairs; int lowest = INT16_MAX; for(int i=0; i<arr.size(); i++){ if((i+1)<arr.size()){ int diff = arr[i+1]-arr[i]; if(diff==lowest){ pairs.push_back(arr[i]); pairs.push_back(arr[i+1]); } if (diff<lowest) { lowest = diff; pairs.clear(); pairs.push_back(arr[i]); pairs.push_back(arr[i+1]); } } } return pairs; }
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 →