class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (nums.size() < 2) return false;
if (t < 0) return false;
map<int, int> m1;
map<int, int> m2;
for (int i = 0; i < nums.size(); i++){
int num1 = nums[i] >= 0 ? nums[i]/(t+1) : nums[i]/(t+1)  1;
if (m1.count(num1) && m1[num1] >= i  k) return true;
m1[num1] = i;
int num2 = nums[i] >= 0 ? (nums[i] + (t+2) / 2 )/(t+1) : (nums[i] + (t+2) / 2 )/(t+1)  1;
if (m2.count(num2) && m2[num2] >= i  k) return true;
m2[num2] = i;
}
return false;
}
};
My Accepted O(n) Solution > simplify this problem into Contain Duplicate II


class Solution { public: bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { if (nums.size() < 2) return false; if (t < 0) return false; unordered_map<int, int> m1; for (int i = 0; i < nums.size(); i++){ int num1 = nums[i] >= 0 ? nums[i]/(t+1) : nums[i]/(t+1)  1; if (m1.count(num1) && m1[num1] >= i  k) return true; m1[num1] = i; if(m1.count(num1+1)) { if(labs((long)nums[i]nums[m1[num1+1]])<=(long)t&&m1[num1+1] >= i  k)return 1; } if(m1.count(num11)) { if(labs((long)nums[i]nums[m1[num11]])<=(long)t&&m1[num11] >= i  k)return 1; } } return false; } };