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

# Maximum Gap Python Solution with Tests

> Tested Python solution for LeetCode 164 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 164, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sorting](/catalog/topics/sorting), Bucket Sort, Radix Sort, Pigeonhole Principle. [View on LeetCode](https://leetcode.com/problems/maximum-gap/description/).

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

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

## Problem

Given an integer array \<code>nums\</code>, return \<em>the maximum difference between two successive elements in its sorted form\</em>. If the array contains less than two elements, return \<code>0\</code>.

You must write an algorithm that runs in linear time and uses linear extra space.

### Examples

```
Input: nums = [3,6,9,1]
Output: 3
```

**Explanation:** The sorted form of the array is \[1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.

```
Input: nums = [10]
Output: 0
```

**Explanation:** The array contains less than 2 elements, therefore return 0.

### Constraints

* `1 <= nums.length <= 10^5`
* `0 <= nums[i] <= 10^9`

**Follow up:** Could you solve it without using any built-in sorting function?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def maximum_gap(self, nums: list[int]) -> int:
        n = len(nums)
        if n < 2:
            return 0

        low, high = min(nums), max(nums)
        if low == high:
            return 0

        # Pigeonhole: the answer is at least ceil((high - low) / (n - 1)),
        # so buckets narrower than that guarantee the max gap spans buckets.
        size = max(1, (high - low) // (n - 1))
        count = (high - low) // size + 1
        bucket_min: list[int | None] = [None] * count
        bucket_max: list[int | None] = [None] * count

        for num in nums:
            idx = (num - low) // size
            lo = bucket_min[idx]
            hi = bucket_max[idx]
            if lo is None or hi is None:
                bucket_min[idx] = num
                bucket_max[idx] = num
            elif num < lo:
                bucket_min[idx] = num
            elif num > hi:
                bucket_max[idx] = num

        result = 0
        prev_max = low
        for idx in range(count):
            cur_min = bucket_min[idx]
            cur_max = bucket_max[idx]
            if cur_min is None or cur_max is None:
                continue
            result = max(result, cur_min - prev_max)
            prev_max = cur_max
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


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