Given an integer array nums and two integers k and t, return true if there are two distinct indices i and j in the array such that abs(nums[i] - nums[j]) &lt;= t and abs(i - j) &lt;= k.

&nbsp;
Example 1:
Input: nums = [1,2,3,1], k = 3, t = 0
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1, t = 2
Output: true
Example 3:
Input: nums = [1,5,9,1,5,9], k = 2, t = 3
Output: false

&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 2 * 104
	-231 &lt;= nums[i] &lt;= 231 - 1
	0 &lt;= k &lt;= 104
	0 &lt;= t &lt;= 231 - 1


from sortedcontainers import SortedList
class Solution:
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        sl = SortedList()
        for i in range(len(nums)):
            if i > k: sl.remove(nums[i-k-1])
            idxl = sl.bisect_left(nums[i]-t)
            idxr = sl.bisect_right(nums[i]+t)
            if idxl != idxr: return True
            sl.add(nums[i])
        return False
/**
 * Sliding Window solution using Buckets
 *
 * Time Complexity: O(N)
 *
 * Space Complexity: O(min(N, K+1))
 *
 * N = Length of input array. K = Input difference between indexes.
 */
class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if (nums == null || nums.length < 2 || k < 1 || t < 0) {
            return false;
        }

        HashMap<Long, Long> buckets = new HashMap<>();
        // The bucket size is t+1 as the ranges are from 0..t, t+1..2t+1, ..
        long bucketSize = (long) t + 1;

        for (int i = 0; i < nums.length; i++) {
            // Making sure only K buckets exists in map.
            if (i > k) {
                long lastBucket = ((long) nums[i - k - 1] - Integer.MIN_VALUE) / bucketSize;
                buckets.remove(lastBucket);
            }

            long remappedNum = (long) nums[i] - Integer.MIN_VALUE;
            long bucket = remappedNum / bucketSize;

            // If 2 numbers belong to same bucket
            if (buckets.containsKey(bucket)) {
                return true;
            }

            // If numbers are in adjacent buckets and the difference between them is at most
            // t.
            if (buckets.containsKey(bucket - 1) && remappedNum - buckets.get(bucket - 1) <= t) {
                return true;
            }
            if (buckets.containsKey(bucket + 1) && buckets.get(bucket + 1) - remappedNum <= t) {
                return true;
            }

            buckets.put(bucket, remappedNum);
        }

        return false;
    }
}
class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {
        int i=0;
        map<int,int> mp;
        int n=nums.size();
        for(int j=0;j<n;j++){
            auto val=mp.lower_bound(nums[j]);
            if(val!=mp.end() and (val->first-nums[j])<=valueDiff){
                return true;
            }
            if(val!=mp.begin()){
                val--;
                if(abs(val->first-nums[j])<=valueDiff){
                    return true;
                }
            }
            mp[nums[j]]++;
            if((j-i)==indexDiff){
                mp[nums[i]]--;
                if(mp[nums[i]]==0){
                    mp.erase(nums[i]);
                }
                i++;
            }
        }
        return false;
    }
};
/**
 * @param {number[]} nums
 * @param {number} k
 * @param {number} t
 * @return {boolean}
 */
var containsNearbyAlmostDuplicate = function(nums, k, t) {
   for(let i=0;i<nums.length;i++){
        for(let j=i+1;j<nums.length;j++){
            if(Math.abs(nums[i]-nums[j])<=t && (Math.abs(i-j)<=k)){
                return true;
            }
        }
   }
    return false;
};
Contains Duplicate III