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

# Smallest Range II Python Solution with Tests

> Tested Python solution for LeetCode 910 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 910, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/smallest-range-ii/description/).

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

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

## Problem

You are given an integer array `nums` and an integer `k`.

For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.

The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.

Return *the minimum **score** of* `nums` *after changing the values at each index*.

### Examples

```
Input: nums = [1], k = 0
Output: 0
Explanation: The score is max(nums) - min(nums) = 1 - 1 = 0.
```

```
Input: nums = [0,10], k = 2
Output: 6
Explanation: Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
```

```
Input: nums = [1,3,6], k = 3
Output: 3
Explanation: Change nums to be [4, 6, 3]. The score is max(nums) - min(nums) = 6 - 3 = 3.
```

### Constraints

* 1 \<= nums.length \<= 10^4
* 0 \<= nums\[i] \<= 10^4
* 0 \<= k \<= 10^4

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n) for the sort, then a single linear sweep
    # Space: O(1) beyond the sort
    def smallest_range_ii(self, nums: list[int], k: int) -> int:
        nums = sorted(nums)
        result = nums[-1] - nums[0]
        low = nums[0] + k
        high = nums[-1] - k
        for i in range(len(nums) - 1):
            big = max(nums[i] + k, high)
            small = min(nums[i + 1] - k, low)
            result = min(result, big - small)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) for the sort, then a single linear sweep | O(1) beyond the sort |

## Tags


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