<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p>

<p>Find a pair of indices <code>(i, j)</code> such that:</p>

<ul>
	<li><code>i != j</code>,</li>
	<li><code>abs(i - j) &lt;= indexDiff</code>.</li>
	<li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li>
</ul>

<p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> We can choose (i, j) = (0, 3).
We satisfy the three conditions:
i != j --&gt; 0 != 3
abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3
abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
<strong>Output:</strong> false
<strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= indexDiff &lt;= nums.length</code></li>
	<li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def containsNearbyAlmostDuplicate(self, nums: List[int], indexDiff: int, valueDiff: int) -> bool:
        # brute force
        # n = len(nums)
        # for i in range(n):
        #     for j in range(i+1, n):
        #         if abs(i-j) <= indexDiff and abs(nums[i] - nums[j]) <= valueDiff:
        #             return True
        # return False

        # sliding window
        if valueDiff < 0:  # Edge case where valueDiff is negative
            return False

        # Helper function to calculate the bucket ID, handling negative numbers correctly
        def get_bucket_id(x, bucket_size):
            return x // bucket_size 
        
        seen = {}  # Dictionary to store the bucket ID and the corresponding (index, value)
        bucket_size = valueDiff + 1  # Bucket size is valueDiff + 1 to ensure proper bucketing

        for i, x in enumerate(nums):
            # Determine the bucket ID for the current number
            bucket_id = get_bucket_id(x, bucket_size)

            # Check if the current bucket already contains a number
            if bucket_id in seen and i - seen[bucket_id][0] <= indexDiff:
                return True

            # Check the previous bucket (bucket_id - 1)
            if bucket_id - 1 in seen and i - seen[bucket_id - 1][0] <= indexDiff and abs(x - seen[bucket_id - 1][1]) <= valueDiff:
                return True

            # Check the next bucket (bucket_id + 1)
            if bucket_id + 1 in seen and i - seen[bucket_id + 1][0] <= indexDiff and abs(x - seen[bucket_id + 1][1]) <= valueDiff:
                return True

            # Store the current number and its index in the seen dictionary
            seen[bucket_id] = (i, x)

            # Remove the element that's outside the indexDiff range
            if i >= indexDiff:
                old_bucket_id = get_bucket_id(nums[i - indexDiff], bucket_size)
                del seen[old_bucket_id]

        return False
40938