Minimum Loss Time : O(nlogn) Space : O(n) 123456789101112int minimumLoss(vector<long> A) { long res = INT_MAX, n = A.size(); set<long> s; for(int i = n - 1; i >= 0; i--) { auto it = s.lower_bound(A[i]); if(it != begin(s)) res = min(res, A[i] - *prev(it)); s.insert(A[i]); } return res;}