<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>

def get_bucket_id(num, value_diff):
    if value_diff == 0:
        return num
    return num//value_diff

def validate_diff(num1, num2, value_diff):
    return abs(num1 - num2) <= value_diff

def duplicate_in_bucket_slot(buckets, id, num, value_diff):
    first_check = id in buckets
    if first_check:
        return True
    lower_neighbour_id = id - 1
    lower_check = lower_neighbour_id in buckets
    if lower_check:
        lower_num = buckets[lower_neighbour_id]
        found_duplicate = validate_diff(num1=num, num2=lower_num, value_diff=value_diff)
        if found_duplicate:
            return True
    higher_neighbour_id = id + 1
    higher_check = higher_neighbour_id in buckets
    if higher_check:
        higher_num = buckets[higher_neighbour_id]
        found_duplicate = validate_diff(num1=num, num2=higher_num, value_diff=value_diff)
        if found_duplicate:
            return True
    return False

def delete_bucket_item(num, buckets, value_diff):
    id = get_bucket_id(num, value_diff)
    del buckets[id]
    

class Solution:
    def containsNearbyAlmostDuplicate(self, nums, indexDiff: int, valueDiff: int) -> bool:
        num_index = 0
        buckets = {}
        while num_index < len(nums):
            current_num = nums[num_index]
            bucket_id = get_bucket_id(current_num, valueDiff)
            if duplicate_in_bucket_slot(
                buckets=buckets,
                id=bucket_id,
                num=current_num,
                value_diff=valueDiff
            ):
                return True
            buckets[bucket_id] = current_num
            is_at_start = num_index < indexDiff
            if is_at_start:
                pass
            else:
                index_to_remove = num_index - indexDiff
                delete_bucket_item(
                    num=nums[index_to_remove],
                    buckets=buckets,
                    value_diff=valueDiff
                )
            num_index += 1
        return False
33399