You are viewing a single comment's thread. Return to all comments →
Here is my c++ solution, you can watch the explanation here: https://youtu.be/-sX3IgdQ6Wg
vector<int> closestNumbers(vector<int> arr) { sort(arr.begin(), arr.end()); int diff = INT_MAX; vector<int> result; for(int i = 1; i < arr.size(); i++){ if(arr[i] - arr[i-1] < diff){ diff = arr[i] - arr[i-1]; result = {arr[i-1], arr[i]}; } else if(arr[i] - arr[i-1] == diff){ result.insert(result.end(), {arr[i-1], arr[i]}); } } return result; }
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 →
Here is my c++ solution, you can watch the explanation here: https://youtu.be/-sX3IgdQ6Wg