1200. Minimum Absolute Difference

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
vector<vector<int>> ret;
sort(arr.begin(),arr.end());
int mn_abs = arr[1] - arr[0], n = arr.size();
for(int i=1;i<n ; ++i) mn_abs = min(mn_abs, arr[i] - arr[i-1]);
for(int i=1;i<n;++i){
if(arr[i] - arr[i-1] == mn_abs) ret.push_back({arr[i-1], arr[i]});
}
return ret;
}
};

analysis

  • time complexity O(nlogn)
  • space complexity O(1)