1 class Solution { 2 public: 3 bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { 4 vector<pair<long, long>> res; 5 for(int i=0; i<nums.size(); i++) 6 res.push_back(make_pair(nums[i],i)); 7 8 sort(res.begin(), res.end()); 9 10 for(int j=0; j<nums.size(); j++) 11 { 12 int most = j+1; 13 while(most<nums.size() && res[most].first-res[j].first<=t) 14 { 15 if(abs(res[most].second-res[j].second) <= k) 16 return true; 17 most++; 18 } 19 20 } 21 return false; 22 } 23 };