> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Contains Duplicate III Python Solution

> Tested Python solution for LeetCode 220 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 220, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Sliding Window](/catalog/topics/sliding-window), [Sorting](/catalog/topics/sorting), Bucket Sort, [Ordered Set](/catalog/topics/ordered-set). [View on LeetCode](https://leetcode.com/problems/contains-duplicate-iii/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 220   # by problem number
lcpy gen -s contains_duplicate_iii   # by problem name
```

## Problem

You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.

Find a pair of indices `(i, j)` such that:

* `i != j`,
* `abs(i - j) <= indexDiff`, and
* `abs(nums[i] - nums[j]) <= valueDiff`.

Return `true` *if such pair exists or* `false` *otherwise*.

### Examples

```
Input: nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
Output: true
Explanation: We can choose (i, j) = (0, 3).
i != j, abs(i - j) <= indexDiff, abs(nums[i] - nums[j]) <= valueDiff
```

```
Input: nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
Output: false
Explanation: No pair of indices satisfies all three conditions.
```

### Constraints

* 2 \<= nums.length \<= 10^5
* -10^9 \<= nums\[i] \<= 10^9
* 1 \<= indexDiff \<= nums.length
* 0 \<= valueDiff \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/contains_duplicate_iii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/contains_duplicate_iii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(min(n, index_diff))
    def contains_nearby_almost_duplicate(
        self, nums: list[int], index_diff: int, value_diff: int
    ) -> bool:
        width = value_diff + 1
        buckets: dict[int, int] = {}
        for i, num in enumerate(nums):
            if i > index_diff:
                del buckets[nums[i - index_diff - 1] // width]
            bucket = num // width
            if bucket in buckets:
                return True
            if bucket - 1 in buckets and num - buckets[bucket - 1] <= value_diff:
                return True
            if bucket + 1 in buckets and buckets[bucket + 1] - num <= value_diff:
                return True
            buckets[bucket] = num
        return False
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(min(n, index\_diff)) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.